Go环境搭建
Go 环境搭建参看:https://blog.csdn.net/sunxianghuang/article/details/87808103
下载安装Gin框架
$ go get -u github.com/gin-gonic/gin
Gin框架使用示例
GinTest.go源码
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
启动Web服务器
# go run GinTest.go
访问链接http://localhost:8080/ping
在另外一个Linux终端访问HTTP链接,即可得到HTTP响应
#curl http://localhost:8080/ping
{"message":"pong"}
安装失败参考
http://wiki.jikexueyuan.com/project/go-command-tutorial/0.3.html
https://segmentfault.com/a/1190000016310831?utm_source=tag-newest
背景
由于各种问题,国内使用 go get 安装 golang 官方包可能会失败,如我自己在安装 collidermain 时,出现了以下报错:
$ go get collidermain
package golang.org/x/net/websocket: unrecognized import path
"golang.org/x/net/websocket" (https fetch: Get https://golang.org/x/net/websocket?go-get=1:
dial tcp 216.239.37.1:443: i/o timeout)
原理
其实 golang 在 github 上建立了一个镜像库,如 https://github.com/golang/net 即是 https://golang.org/x/net 的镜像库
获取 golang.org/x/net 包,其实只需要以下步骤:
mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/net.git
其它 golang.org/x 下的包获取皆可使用该方法