21-day Boutique blockchain free learning, in-depth expert led the way, to help developers easily play the blockchain! >>>
Reference Documentation:
- Golang Micro-Frame Gin Introduction https://www.jianshu.com/p/a31e4ee25305
First, the use of Govendor
Use a vendor tool like Govendorgo get Govendor (install)
$ go get github.com/kardianos/govendor
Create your project folder and CD inside (creating a local project directory and cutting to that directory)
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
Vendor init your project and add gin (generate Vendor file and Vendor.json, and download gin)
$ govendor init$ govendor fetch github.com/gin-gonic/gin@v1.2
Copy a starting template inside your project (copy Https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic /main.go file to local, measured local main.go is empty, manually copy from $gopath/src/github.com/gin-gonic/gin/examples/basic directory)
$ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go
Run your project
$ go run main.go
Two, Path parameter (Parameters in path) test
When the parameter is in the URL? Before the time, by the value: = C.param ("key") way to get the parameters, when the parameters in the URL? Later, use the value: = C.qury ("Key") method. If swapped, a null character is returned. The source code is as follows:
package mainimport ("github.com/gin-gonic/gin""net/http")func main() {router := gin.Default();router.GET("/welcome/:user/*action", func(c *gin.Context){user := c.Param("user")action := c.Param("action")name := c.DefaultQuery("name", "uname")password := c.Query("password")age := c.Query("age")c.String(http.StatusOK, "user=%s, action=%s, name=%s, password=%s, age=%s\n", user, action, name, password, age)})router.Run()}
The test method is as follows:
$ curl -X GET http://127.0.0.1:8080/welcome/Guest/findsomething?dname\=jerry\&password\=123\&age\=90user=Guest, action=/findsomething, name=uname, password=123, age=90