一、使用 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)测试

源代码如下:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	router := gin.Default()

	router.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })

	router.Run()
}

测试方法如下:

$ curl  -X GET  http://127.0.0.1:8080/user/jerry
Hello jerry


 转载至链接:https://my.oschina.net/u/3045933/blog/1862491