使用go module管理go package - Thu, Jul 11, 2019
使用go module管理go包并编译
1. 概述
go版本进行了升级,目前1.12.5(官方稳定版1.12.7,官方测试版1.13.1beta1)。 go语言从1.11开始支持go mod进行包管理,1.12时将go mod进行了转正。所以使用go mod进行包管理是大势所趋。
2. go mod命令
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.
3. 从原有的vendor进行迁移
3.1 不删除vendor.json
如果不删除vendor.json,go mod会从json文件读取版本,并进行cache
go mod init
go mod tidy
3.2 删除vendor
删除vendor之后,go mod会遍历代码,获取包然后从远程服务器进行缓存
go mod init
go mod tidy
4. 关于墙
由于众所周知的原因,在大陆访问google的服务会被墙无法访问。 但是go语言又是google开发的语言,所以go语言大多数的包都无法访问。
4.1 使用replace
我们可以使用replace,将google的包替换为我们能访问的服务器上的包
replace (
cloud.google.com/go => github.com/googleapis/google-cloud-go v0.40.0
golang.org/x/crypto => github.com/golang/crypto v0.0.0-20190611184440-5c40567a22f8
golang.org/x/exp => github.com/golang/exp v0.0.0-20190510132918-efd6b22b2522
golang.org/x/image => github.com/golang/image v0.0.0-20190523035834-f03afa92d3ff
golang.org/x/lint => github.com/golang/lint v0.0.0-20190409202823-959b441ac422
golang.org/x/mobile => github.com/golang/mobile v0.0.0-20190607214518-6fa95d984e88
golang.org/x/net => github.com/golang/net v0.0.0-20190611141213-3f473d35a33a
golang.org/x/oauth2 => github.com/golang/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sync => github.com/golang/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys => github.com/golang/sys v0.0.0-20190610200419-93c9922d18ae
golang.org/x/text => github.com/golang/text v0.3.2
golang.org/x/time => github.com/golang/time v0.0.0-20190308202827-9d24e82272b4
golang.org/x/tools => github.com/golang/tools v0.0.0-20190612232758-d4e310b4a8a5
google.golang.org/api => github.com/googleapis/google-api-go-client v0.6.0
google.golang.org/appengine => github.com/golang/appengine v1.6.1
google.golang.org/genproto => github.com/google/go-genproto v0.0.0-20190611190212-a7e196e89fd3
google.golang.org/grpc => github.com/grpc/grpc-go v1.21.1
)
4.2 使用开放代理
# Enable the go modules feature
export GO111MODULE=on
# Set the GOPROXY environment variable
export GOPROXY=https://goproxy.io