设置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
不当之处,请予指正。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。