查漏补缺之go依赖管理+go mod引用本地包+go module原理深入

vendor

使用vendor进行包管理,首先要保证项目在$GOPATH/src/路径下(踩过坑),然后build时就会按照如图所示的优先级进行包的搜索。

一个没有找到包的实例:

module

GO 依赖管理工具go Modules(官方推荐)

  • 主要步骤
1. 打开 Go modules
go env -w GO111MODULE=on

2. 设置 GOPROXY
go env -w GOPROXY=https://goproxy.cn,direct

3. 创建项目,生成go.mod文件
go mod init  demo

4. go build
4.1 项目中增加了go.sum,编译好的可执行文件
4.2 go.mod文件内容发生了变化,增加了
require github.com/gin-gonic/gin v最新版本

5. 更换依赖版本
go mod edit -require="github.com/gin-gonic/gin@新版本号(即tags)"
go tidy #更新现有依赖
  1. go mod init
  2. go mod

go mod 的 require 和 replace // 附录3

go mod 私有仓库 // 附录4

配置.netrc

其他

go list -f '{{.Deps}}' | tr "[" " " | tr "]" " " | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}'

深入

// 附录2

参考资料

1.How make go import packages from vendor?
2.Go Modules 和 Go Proxy
3.Go Modules 内部分享
4.Go Module 常见问题- 私有仓库

posted @ 2020-01-17 15:27  sicnu-yudidi  阅读(967)  评论(0编辑  收藏  举报