设置gopath
D:\work\goPath
安装gin
go get -u github.com/gin-gonic/gin
报错:
go get: module github.com/gin-gonic/gin: Get "https://proxy.golang.org/github.com/gin-gonic/gin/@v/list":
dial tcp 216.58.200.241: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.
开启gomodule
go env -w GO111MODULE=on
设置goproxy
go env -w goproxy=https://goproxy.cn
go env
再次安装gin
go get -u github.com/gin-gonic/gin
创建go项目
因为开启gomodule支持,项目不能在gopath下,
不然会报错:
$GOPATH/go.mod exists but should not
D:\work\goWorkspace\src\helloGin
创建main.go
无法引入gin
修改main.go并运行
package main
import "github.com/gin-gonic/gin"
func main() {
engine := gin.Default()
engine.GET("/", func(context *gin.Context) {
context.String(200, "hello, gin")
})
engine.Run()
}
报错:
no required module provides package github.com/gin-gonic/gin;
配置项目gopath
D:\work\goWorkspace
配置gomodule
(否则编辑器提示不可用)
D:\work\goWorkspace\src\helloGin
cmd
go mod init gin
go mod edit -require github.com/gin-gonic/gin@latest
go mod tidy
否则可能还会出现以下错误:
go: github.com/gin-gonic/gin@v1.7.1: missing go.sum entry;
再次运行main.go
然后项目gopath下会多出pkg路径
helloGin下多出go.mod、go.sum
引入gin正常
hello gin
不当之处,请予指正。