1.首先就是要配置GOPROXY,编辑/etc/environment文件,加上export GOPROXY=https://mirrors.aliyun.com/goproxy/和export GO111MODULE="on"
2.记得source /etc/environment,则在此控制台执行go env会发现这两个参数变了(但是在其他控制台又不会,需要再次执行source /etc/environment;或者可以重启一下)
3.在go.mod文件里通过require来添加依赖包(目前是通过git的形式添加依赖,latest会找最新的release的tag,如果没有则用最新的commit)
分别有三种require方式:
require (
github.com/casbin/casbin/v2 v2.0.2
github.com/fastly/go-utils d95a45783239
github.com/gin-gonic/gin latest
)
第一种则是用特定的版本,第二种是某次提交的版本,第三种则是由go module去找最新的release tag版本,如果没有则用最新的commit版本;
在导入包的时候则不需要指定版本,因为在go.mod里已经指定了(go.mod其实类似pom.xml)
4.go mod的命令
download download modules to local cache(下载依赖包)
edit edit go.mod from tools or scripts(编辑go.mod
graph print module requirement graph (打印模块依赖图)
init initialize new module in current directory(在当前目录初始化mod)
tidy add missing and remove unused modules(拉取缺少的模块,移除不用的模块)
vendor make vendored copy of dependencies(将依赖复制到vendor下)
verify verify dependencies have expected content (验证依赖是否正确)
why explain why packages or modules are needed(解释为什么需要依赖)
原文:https://www.cnblogs.com/silentdoer/p/12768977.html