目录
requirement
go module
是在go 1.11版本才开始有的,需要将环境变量 GO111MODULE
设置为 on
才能正常使用文章源自编程技术分享-https://mervyn.life/1eefbb84.html
例:文章源自编程技术分享-https://mervyn.life/1eefbb84.html
export GO111MODULE=on
初次使用
Go1.11新增了命令 go mod
用于支持 module
。文章源自编程技术分享-https://mervyn.life/1eefbb84.html
➜ demo go help mod
Go mod provides access to operations on modules.
Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.
Usage:
go mod <command> [arguments]
The commands are:
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
Use "go help mod <command>" for more information about a command.
- 创建项目 demo
cd ~/go && mkdir demo && cd demo
- 初始化 mod
go mod init
注意: 如果创建的项目 demo
所属目录不再 GOPATH
内, 则 init 时需要制定名字文章源自编程技术分享-https://mervyn.life/1eefbb84.html
go mod init demo
这时目录中会出现一个 go.mod
的文件文章源自编程技术分享-https://mervyn.life/1eefbb84.html
module demo
go 1.12
require (
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 // indirect
github.com/gin-gonic/gin v1.3.0
github.com/golang/protobuf v1.3.0 // indirect
github.com/mattn/go-isatty v0.0.6 // indirect
github.com/ugorji/go/codec v0.0.0-20190309163734-c4a1c341dc93 // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
- 新建入口文件
main.go
package main
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()
}
- 执行
go build
执行完目录中会出现一个名为 go.sum
的文件,内容如下:文章源自编程技术分享-https://mervyn.life/1eefbb84.html
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/golang/protobuf v1.3.0 h1:kbxbvI4Un1LUWKxufD+BiE6AEExYYgkQLQmLFqA1LFk=
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA=
github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/ugorji/go v1.1.2 h1:JON3E2/GPW2iDNGoSAusl1KDf5TRQ8k8q7Tp097pZGs=
github.com/ugorji/go v1.1.2/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
github.com/ugorji/go/codec v0.0.0-20190309163734-c4a1c341dc93 h1:JnDJ9gMf6CfErtoOXnghtY5hhMuDtW4tUBaWSBrqvKs=
github.com/ugorji/go/codec v0.0.0-20190309163734-c4a1c341dc93/go.mod h1:iT03XoTwV7xq/+UGwKO3UbC1nNNlopQiY61beSdrtOA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
go.sum
是一个模块版本内容的校验值,用来验证当前缓存的模块。go.sum
包含了直接依赖和间接依赖的包的信息,比 go.mod
要多一些。文章源自编程技术分享-https://mervyn.life/1eefbb84.html
此时的代码存放在 $GOPATH/pkg/mod
目录下文章源自编程技术分享-https://mervyn.life/1eefbb84.html
go.mod 指令
go.mod
文件有四种指令:module,require,exclude,replace。文章源自编程技术分享-https://mervyn.life/1eefbb84.html
- module:模块名称
- require:依赖包列表以及版本
- exclude:禁止依赖包列表(仅在当前模块为主模块时生效)
- replace:替换依赖包列表 (仅在当前模块为主模块时生效)
其他命令
go mod tidy //拉取缺少的模块,移除不用的模块。
go mod download //下载依赖包
go mod graph //打印模块依赖图
go mod vendor //将依赖复制到vendor下
go mod verify //校验依赖
go mod why //解释为什么需要依赖
go list -m -json all //依赖详情
文章源自编程技术分享-https://mervyn.life/1eefbb84.html
评论