Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance – up to 40 times faster. If you need smashing performance, get yourself some Gin.
$ go get -u -v github.com/ramya-rao-a/go-outline github.com/ramya-rao-a/go-outline (download) Fetching https://golang.org/x/tools/go/buildutil?go-get=1 https fetch failed: Get https://golang.org/x/tools/go/buildutil?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
因此,可以先从 Github 手动安装好,再安装 go-outline 和 goreturns。
1 2 3 4
git clone https://github.com/golang/tools.git $GOPATH/src/golang.org/x/tools go get -v github.com/ramya-rao-a/go-outline go get -v github.com/sqs/goreturns go get -v github.com/rogpeppe/godef
$ go run main.go [GIN-debug] GET / --> main.main.func1 (3 handlers) [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080
浏览器访问 http://localhost:8080
路由(Route)
路由方法有 GET, POST, PUT, PATCH, DELETE 和 OPTIONS,还有Any,可匹配以上任意类型的请求。
无参数
1 2 3 4
r.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "Who are you?") })
r.GET("/users", func(c *gin.Context) { name := c.Query("name") role := c.DefaultQuery("role", "teacher") c.String(http.StatusOK, "%s is a %s", name, role) })
1 2
$ curl "http://localhost:9999/users?name=Tom&role=student" Tom is a student
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance – up to 40 times faster. If you need smashing performance, get yourself some Gin.
$ go get -u -v github.com/ramya-rao-a/go-outline github.com/ramya-rao-a/go-outline (download) Fetching https://golang.org/x/tools/go/buildutil?go-get=1 https fetch failed: Get https://golang.org/x/tools/go/buildutil?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
因此,可以先从 Github 手动安装好,再安装 go-outline 和 goreturns。
1 2 3 4
git clone https://github.com/golang/tools.git $GOPATH/src/golang.org/x/tools go get -v github.com/ramya-rao-a/go-outline go get -v github.com/sqs/goreturns go get -v github.com/rogpeppe/godef
$ go run main.go [GIN-debug] GET / --> main.main.func1 (3 handlers) [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080
浏览器访问 http://localhost:8080
路由(Route)
路由方法有 GET, POST, PUT, PATCH, DELETE 和 OPTIONS,还有Any,可匹配以上任意类型的请求。
无参数
1 2 3 4
r.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "Who are you?") })
r.GET("/users", func(c *gin.Context) { name := c.Query("name") role := c.DefaultQuery("role", "teacher") c.String(http.StatusOK, "%s is a %s", name, role) })
1 2
$ curl "http://localhost:9999/users?name=Tom&role=student" Tom is a student