Golang modules包依赖管理工具

初始化

执行go mod  init module-name,其中module-name为包名字,执行完后会生成go.mod文件,如下

module module-name

go 1.13

拉取依赖

go get ***

如:go get  github.com/jinzhu/gorm

更多参数

用 go get 拉取新的依赖
拉取最新的版本(优先择取 tag):go get golang.org/x/text@latest
拉取 master 分支的最新 commit:go get golang.org/x/text@master
拉取 tag 为 v0.3.2 的 commit:go get golang.org/x/text@v0.3.2
拉取 hash 为 342b231 的 commit,最终会被转换为 v0.3.2:go get golang.org/x/text@342b2e
用 go get -u 更新现有的依赖
用 go mod download 下载 go.mod 文件中指明的所有依赖
用 go mod tidy 整理现有的依赖
用 go mod graph 查看现有的依赖结构
用 go mod init 生成 go.mod 文件 (Go 1.13 中唯一一个可以生成 go.mod 文件的子命令)
用 go mod edit 编辑 go.mod 文件
用 go mod vendor 导出现有的所有依赖 (事实上 Go modules 正在淡化 Vendor 的概念)
用 go mod verify 校验一个模块是否被篡改过

包管理

使用go build,go test以及go list时,go会自动得更新go.mod文件,将依赖关系写入其中。

如果想手动处理依赖关系,那么使用如下的命令:

go mod tidy

初始化项目

go get拉取新的依赖,如引入go-gin

go mod init github.com/jihite/go-gin-example

执行完后,go.mod如下

module github.com/jihite/go-gin-example

go 1.13

require (
    github.com/gin-gonic/gin v1.5.0
    github.com/go-playground/universal-translator v0.17.0 // indirect
    github.com/golang/protobuf v1.3.3 // indirect
    github.com/json-iterator/go v1.1.9 // indirect
    github.com/leodido/go-urn v1.2.0 // indirect
    github.com/mattn/go-isatty v0.0.12 // indirect
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.1 // indirect
    golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
    gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
    gopkg.in/yaml.v2 v2.2.8 // indirect
)

其中indirect 是非直接依赖

同时多了go.sum文件,如下

github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=

go.sum罗列了当前项目直接和间接依赖的所有模块版本,格式

模块 版本号 SHA-256 哈希值

 

安装指定依赖

go get -u github.com/smartwalle/dbs 

 

更新

go get package[@version]

示例

go get  github.com/gin-gonic/gin@master

 

posted @ 2020-02-04 21:04  jihite  阅读(3142)  评论(1编辑  收藏  举报