This article is introduced to you by the go language tutorial column. The theme is about the pitfalls encountered in Gin installation. I hope it will be helpful to friends in need!
1. Install the official document executiongo get -u github.com/gin-gonic/gin
Because I can't circumvent the wall, Baidu said to use gopm to solve it. Friends in the group said that this kind of experiment has long been outdated, and now they use go mod to solve it.
So start go mod
go env -w GOBIN=/Users/youdi/go/bin go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct // 使用七牛云的
Use go mod to manage a new project
mkdir Gone cd Gone go mod init Gone
Check the go.mod file
module Gone go 1.16
Once the go.mod file is created , its content will be fully controlled by the go toolchain. The go toolchain will modify and maintain the go.mod file when various commands are executed, such as go get, go build, go mod, etc.
go.mod provides four commands: module, require, replace and exclude
module
The statement specifies the name (path) of the packagerequire
The dependency module specified by the statement replace
The statement can replace the dependency module exclude
The statement can ignore the dependency Item modulepackage mainimport ( "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 (for windows "localhost:8080")}
Execute go run main.go. When running the code, you will find that go mod will automatically find dependencies and automatically download
The result is an error
main.go:3:8: no required module provides package github.com/gin-gonic/gin; to add it: go get github.com/gin-gonic/gin
Execute: go mod edit -require github .com/gin-gonic/gin@latest Solution, specify the Gin version
Run go run main.go again and report an error
go: github.com/gin-gonic/gin@v1.7.4: missing go.sum entry; to add it: go mod download github.com/gin-gonic/gin
Then execute go mod tidy and execute go run main.go again. Finally it runs.
For more golang related knowledge, please visit golangtutorial column!
The above is the detailed content of Avoid the trap! Pitfalls encountered during Gin installation. For more information, please follow other related articles on the PHP Chinese website!