安装gin
在安装gin之前我们需要部署go环境,这里就不多说了,可以参考:https://www.wulaoer.org/?p=885 安装好go环境后,因为我需要在新的目录下做测试gin框架所以,我需要创建一个目录。
[wolf@wulaoer.org 🔥🔥🔥🔥 wolf ]$ mkdir wulaoer-blog [wolf@wulaoer.org 🔥🔥🔥🔥 wolf ]$ cd wulaoer-blog/ [wolf@wulaoer.org 🔥🔥🔥🔥 wulaoer-blog ]$ go mod init wulaoer-blog #初始化项目,初始化成功后会在项目中创建一个go.mod文件 go: creating new go.mod: module wulaoer-blog [wolf@wulaoer.org 🔥🔥🔥🔥 wulaoer-blog ]$ go mod tidy
为了避免下载gin的时候报错,我们需要在设置一下代理
# 设置goproxy.io代理 go env -w GOPROXY="https://goproxy.io" # 设置GO111MOUDLE go env -w GO111MODULE="on"
上面设置的代理,如果在拉取gin框架的时候出现下面的报错,可以把代理关闭掉,重新拉取即可。
go: github.com/ugorji/go/codec@v1.2.9: verifying module: github.com/ugorji/go/codec@v1.2.9: checking tree#15345421 against tree#15432231: reading https://goproxy.io/sumdb/sum.golang.org/tile/8/1/234: 404 Not Found server response: not found go: github.com/ugorji/go/codec@v1.2.9: verifying module: github.com/ugorji/go/codec@v1.2.9: checking tree#15345421 against tree#15432231: reading https://goproxy.io/sumdb/sum.golang.org/tile/8/1/234: 404 Not Found server response: not found
关闭代理后,重新拉取gin框架,成功了
[wolf@wulaoer.org 🔥🔥🔥🔥 wulaoer-blog ]$ go env -w GOSUMDB=off [wolf@wulaoer.org 🔥🔥🔥🔥 wulaoer-blog ]$ go get -u github.com/gin-gonic/gin go: downloading github.com/ugorji/go v1.2.9 go: downloading github.com/ugorji/go/codec v1.2.9 go: downloading golang.org/x/sys v0.5.0 go: added github.com/gin-contrib/sse v0.1.0 go: added github.com/gin-gonic/gin v1.8.2 go: added github.com/go-playground/locales v0.14.1 go: added github.com/go-playground/universal-translator v0.18.1 go: added github.com/go-playground/validator/v10 v10.11.2 go: added github.com/goccy/go-json v0.10.0 go: added github.com/json-iterator/go v1.1.12 go: added github.com/leodido/go-urn v1.2.1 go: added github.com/mattn/go-isatty v0.0.17 go: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd go: added github.com/modern-go/reflect2 v1.0.2 go: added github.com/pelletier/go-toml/v2 v2.0.6 go: added github.com/ugorji/go/codec v1.2.9 go: added golang.org/x/crypto v0.5.0 go: added golang.org/x/net v0.5.0 go: added golang.org/x/sys v0.5.0 go: added golang.org/x/text v0.6.0 go: added google.golang.org/protobuf v1.28.1 go: added gopkg.in/yaml.v2 v2.4.0
安装成功之后会在go的路径中创建一个gin的代码包。下面编写一个测试文件测试一下是否可以使用gin。
#main.go package main import "github.com/gin-gonic/gin" //个人技术博客 http://www.wulaoer.org func main() { r := gin.Default() //携带基础中间件启动 r.GET("/wulaoer", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "wulaoer-blog", }) }) r.Run() // listen and serve on 0.0.0.0:8080 } [wolf@wulaoer.org 🔥🔥🔥🔥 wulaoer-blog ]$ go run main.go #运行main.go文件
然后重启一个终端窗口,在本地请求一下。
[wolf@wulaoer.org 🔥🔥🔥🔥 ~ ]$ curl http://127.0.0.1:8080/wulaoer {"message":"wulaoer-blog"}%
返回的数据是我们定义的,这说明gin已经安装成功了。没有了,看些其他的吧。
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏