go get 命令可以借助代码管理工具通过远程拉取或更新代码包及其依赖包,并自动完成编译和安装。整个过程就像安装一个 App 一样简单。
这个命令可以动态获取远程代码包,目前支持的有 BitBucket、GitHub、Google Code 和 Launchpad。在使用 go get 命令前,需要安装与远程包匹配的代码管理工具,如 Git、SVN、HG 等,参数中需要提供一个包名。
命令参数
1 | 参数介绍: |
示例
1 | $ go get -insecure -v github.com/gin-gonic/gin |
go get 取包原理
正则匹配出依赖包的查询路径
以包路径github.com/liujianping/foo为例,正则匹配后,得出的查询路径就是:https://github.com/liujianping/foo
再结合go-get参数,向远端VCS系统发起https://github.com/liujianping/foo?go-get=1请求。
查询得出包的远端仓库地址
包的远端仓库地址,可以通过go get请求的响应中的go-import的meta标签中的content中获取的。
示例:
1 | $: curl https://github.com/liujianping/foo?go-get=1 | grep go-import |
根据仓库地址clone到本地
go get命令在go mod目录下与正常目录执行的区别
错误处理
package golang.org/x/crypto/sha3: unrecognized import
package golang.org/x/crypto/sha3: unrecognized import path “golang.org/x/crypto/sha3”: http/https fetch: Get “http://golang.org/x/crypto/sha3?go-get=1": read tcp 172.24.107.53:54865->216.239.37.1:80: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
解决方案,设置代理:
1 | # export GOPROXY=https://goproxy.io |
C:\Users\MrWang\go\src\github.com\gin-gonic\gin\binding\protobuf.go:11:2: cannot find package “github.com/golang/protobuf/proto” in any of:
E:\env\Go\src\github.com\golang\protobuf\proto (from $GOROOT)
C:\Users\MrWang\go\src\github.com\golang\protobuf\proto (from $GOPATH)
C:\Users\MrWang\go\src\github.com\go-playground\validator\baked_in.go:20:2: cannot find package “golang.org/x/crypto/sha3” in any of:
E:\env\Go\src\golang.org\x\crypto\sha3 (from $GOROOT)
C:\Users\MrWang\go\src\golang.org\x\crypto\sha3 (from $GOPATH)
下载指定版本的包
1 | go get github.com/beego/beego/v2@v2.0.0 |