go module
是类似于java
中的maven
,是包的管理工具,在没有这个go module
之前,都是配置本地的GOPATH
,创建的每个项目也都必须创建在这个GOPATH
的src
目录下,且项目的go
文件不能重名
go module
是在go1.1.1
版本推出的
在本地上打开终端执行go env
可看到环境的相关配置:
GO111MODULE
:默认是为空,有三个值,on
(开启),off
(关闭),auto
(自动)
GOPROXY:配置下载包的地址,我们可以更换为国内镜像源
go env -w GO111MODULE=on
设置go module
为开启状态go env -w GOPROXY=https://goproxy.cn,direct
这样go module
就开启好了,开启之后如何使用?先执行go mod help
,不管在学什么只要是有命令行的,先执行help
命令。
可看到其中是有初始化命令,而go module
是用来管理项目的第三方包的,所以我们可以创建一个文件夹,在文件夹中初始化
可看出新的文件 go.mod
不设置的话会报exec: “git”: executable file not found in %PATH%一.git
go get -u github.com/gin-gonic/gin
PS C:\Users\X\Desktop\waodaoxr> go get -u github.com/gin-gonic/gin
go: downloading golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
go: downloading github.com/goccy/go-json v0.9.7
go: downloading github.com/pelletier/go-toml/v2 v2.0.1
go: downloading google.golang.org/protobuf v1.28.0
go: downloading github.com/go-playground/validator/v10 v10.10.0
go: downloading github.com/pelletier/go-toml v1.9.5
go: downloading github.com/pelletier/go-toml/v2 v2.0.6
go: downloading github.com/go-playground/validator/v10 v10.11.1
go: downloading github.com/goccy/go-json v0.9.11
go: downloading golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
go: downloading github.com/go-playground/validator v9.31.0+incompatible
go: downloading github.com/ugorji/go v1.2.7
go: downloading golang.org/x/net v0.2.0
go: downloading golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: downloading golang.org/x/crypto v0.3.0
go: downloading golang.org/x/sys v0.2.0
go: downloading golang.org/x/text v0.4.0
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.8.1
go: added github.com/go-playground/locales v0.14.0
go: added github.com/go-playground/universal-translator v0.18.0
go: added github.com/go-playground/validator/v10 v10.11.1
go: added github.com/goccy/go-json v0.9.11
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.16
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.7
go: added golang.org/x/crypto v0.3.0
go: added golang.org/x/net v0.2.0
go: added golang.org/x/sys v0.2.0
go: added golang.org/x/text v0.4.0
go: added google.golang.org/protobuf v1.28.1
go: added gopkg.in/yaml.v2 v2.4.0
PS C:\Users\X\Desktop\waodaoxr>
下载成功,可查看上面初始化创建的go.mod
文件,可看到导入了一些包
package main
import "github.com/gin-gonic/gin"
func main() {
engine := gin.Default()
engine.GET("/test", func(context *gin.Context) {
context.JSON(200,gin.H{
"message":"the project is test module gin",
})
})
engine.Run()
}
(1)go mod init 项目名称:初始化命令
(2)go mod download : 下载modules到本地cache,路径是go env中的GOMODCACHE,跟java的maven,也就是你的本地仓库
(3)go mod tidy : 添加缺失或删除没有使用的包
(4)go mod verify:验证依赖是否正确
(5)go mod why:查找依赖
(6)go mod edit:可执行go help mod edit查看用法,也可到官网查看,主要也是用来操作go.mod文件的