最近由于工作需要,需要把部分接口迁移到Go语言上,优化部分性能。经过一番考察后,我选择了gin这款轻量级的web框架,用来开发restful api。
首先看一下官方的介绍:
Gin is a web framework written in Go (Golang). It features a martini-> like API with much better performance, up to 40 times faster thanks > to httprouter. If you need performance and good productivity, you > will love Gin.
据说性能是martini的40倍,好吧,martini我也没接触过。
然后看官方给出的一个例子:
1 | package main |
然后运行这个例子, 默认是监听8080端口
1
$ go run example.go
下载和安装
1 | $ go get github.com/gin-gonic/gin |
这里要提到一个小技巧,国内由于各种原因,导致go get 几乎处于不可用的状态,如果你有ss的话,可以使用cow这个工具,把go get 的http请求转发给ss代理,这样在命令行里使用go get 就会比较顺畅了。
- import
1
import "github.com/gin-gonic/gin"
govendor
也可以使用govendor这样的工具,来构建项目。
1.安装govendor
1 | $ go get github.com/kardianos/govendor |
2.创建你的项目文件夹
1 | $ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_" |
3.初始化项目并安装gin
1 | $ govendor init |
4.复制初始模板到你的项目中
1 | $ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go |
5.运行你的项目
1 | $ go run main.go |
OK, 至此呢,一个简单的gin 项目的demo就跑起来了。