Gin 使用笔记
安装
项目名称: gin-test-001
sh
mkdir -p ./gin-test-001
cd gin-test-001
# 初始化
go mod init gin-test-001
# 下载并安装 gin:
go get -u github.com/gin-gonic/gin
touch main.go/main.go
go
package main
// 将 gin 引入到代码中:
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
}sh
# 运行 main.go 并且在浏览器中访问 0.0.0.0:8080/ping
go run main.go报错
go: go.mod file not found in current directory or any parent directory.
sh
# 解决:
go mod init gin-test-001go: module github.com/gin-gonic/gin: Get "https://proxy.golang.org/github.com/gin-gonic/gin/@v/list": dial tcp 142.251.43.17:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
sh
# 解决:
go env -w GOPROXY=https://goproxy.cn,direct