参考文档:
一、使用 Govendor
Use a vendor tool like Govendor go get govendor(安装)
$ go get github.com/kardianos/govendor
Create your project folder and cd inside(创建本地项目目录,并切到该目录下)
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
Vendor init your project and add gin (生成vendor文件以及vendor.json,并下载gin)
$ govendor init
$ govendor fetch github.com/gin-gonic/gin@v1.2
Copy a starting template inside your project (拷贝https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go 文件到本地,实测本地main.go为空,手动从$GOPATH/src/github.com/gin-gonic/gin/examples/basic目录下拷贝即可)
$ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go
Run your project
$ go run main.go
二、路径参数(Parameters in path)测试
当参数在url中?前面的时候,通过 value := c.Param("key")的方式获取参数,当参数在url的?后面的时候,使用 value := c.Qury("key") 方法。如果调换,则返回空字符。 源代码如下:
package main
import (
"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()
}
测试方法如下:
$ curl -X GET http://127.0.0.1:8080/welcome/Guest/findsomething?dname\=jerry\&password\=123\&age\=90
user=Guest, action=/findsomething, name=uname, password=123, age=90
来源:oschina
链接:https://my.oschina.net/u/3045933/blog/1862491