什么是 go module
go module
go 的新依赖管理系统
Go 1.11 和 1.12 初步支持
在
$GOPATH外部
- 前两者通过检测当前文件夹和父文件夹里的 
go.mod 
- 前两者通过检测当前文件夹和父文件夹里的 
 在
$GOPATH内部
- 使用 
GOPATH模式,即使存在go.mod情况下 
- 使用 
 
1.13 默认使用
A collection of Go packages sotred in a file tree with a
go.modfile at its root
- Go 程序通过连接多个packages构成
 - package 由多个源代码文件构成,它们一起声明了常量、类型、变量、函数和它的外部可访问性
 
只出现在 module 的根文件夹
- 子文件夹下的 package 
= <modulel path> + <the subdirectory> - 子文件夹出现 
go.mod将视为独立的 module 
- 子文件夹下的 package 
 执行步骤
解析版本
@<version>@latest@none@upgrade=-u@patch=-u=patch@v1@v2@<branch>@<commit hash>冲突解决
If thereare competing requirements for a particular module, then ‘go get’ resolvesthose requirements by taking the maximum requested version.
go list -m -versions 192.168.1.254/xx/xxxgo list -m -versions all
download if needed
build
install
go命令会自动补全go.mod里缺失的 modules,默认使用@latest版本go命令同时维护着包含特殊 module 版本的密码学哈希的文件go.sumgo.mod和go.sum应该被放到版本管理里A semantic version has three parts: major, minor, and patch.
Each different major version (
v1,
v2, and so on) of a Go module uses a different module path: starting at
v2, the path must end in the major version.
- If an old package and a new package have the same import path, the new package must be backwards compatible with the old package.
 - By definition, a new major version of a package is not backwards compatible with the previous version. This means a new major version of a module must have a different module path than the previous version.
 
go mod init example.com/hello # create a new module, initializing the go.mod file
go list -m all # 列出所有的 packages 和它们的子依赖
go list -m -versions rsc.io/sampler
rsc.io/sampler v1.0.0 v1.2.0 v1.2.1 v1.3.0 v1.3.1 v1.99.99
go get rsc.io/sampler@v1.3.1 # changes the required version of a dependency (or adds a new dependency)
go list -m -versions rsc.io/quote/v3
rsc.io/quote/v3 v3.0.0 v3.1.0
go mod tidy # remove unused dependencies
go package 相关问题
相对导入
go - relative import paths
- An import path beginning with 
./or../is called a relative path. - First, a relative path can be used as a shorthand on the command line.
 - Second, if you are compiling a Go program not in a work space, you can use a relative path in an import statement in that program to refer to nearby code also not in a work space.
 - To avoid ambiguity, Go programs cannot use relative import paths within a work space.
 
- An import path beginning with 
 Code in or below a directory named “internal“ is importable only by code in the directory tree rooted at the parent of “internal”.
vendor directories
- Code in or below a directory named “internal“ is importable only by code in the directory tree rooted at the parent of “internal”.
 
git仓库相关问题
git 支持哪些协议
- local protocol
 - HTTP protocols
- smart HTTP
 - dumb HTTP
 
 - SSH protocol
 - Git protocol
- TCP port 9418
 - no authentication
 - latest version is v2
 
 
如何使用 go get 下载 gitlab 私有项目
内网Gitlab如何鉴权
FAQ - Why does “go get” use HTTPS when cloning a repository - golang.org
- 使用 SSH 私钥
git config --global --add url."git@192.168.1.254:".insteadOf "<http://192.168.1.254/>"~/.gitconfig
 - 使用密码或 token (Github, Gitlab)
~/.netrc
 
- 使用 SSH 私钥
 如何访问 HTTP 而不是 HTTPS
go get -insecure 192.168.1.254/xxx/xxxxgo env -w 'GOINSECURE=*.example.com,*.example2.com'
GOPROXY
- GOPROXY.IO - 一个全球代理 为 Go 模块而生
GO111MODULEGOPROXYGOPRIVATE: a default value forGONOPROXYandGONOSUMDB.
 
GOSUMDB
常见 Go 代码托管仓库
使用问题
go get -insecure xxx@master: xxx@master: invalid version: unknown revision master
- Go version: go1.15.2
 - 可能原因
- Go: go mod获取第三方依赖包 unknown revision xxx 错误解决
- 通过 Install latest Git 解决:失败
 
 - Go 如何解析版本
 - 通过在 
~/.netrc配置内网 gitlab 访问 token 可解决 
 - Go: go mod获取第三方依赖包 unknown revision xxx 错误解决
 
meta data 不匹配
> go get -v git.xxx.com/xx/go-lib-websocket@gomod
go get git.xxx.com/xx/go-lib-websocket@gomod: unrecognized import path "git.xxx.com/xx/go-lib-websocket": parse <http://git.xxx.com/xx/go-lib-websocket?go-get=1:> no go-import meta tags (meta tag 192.168.1.254/xx/go-lib-websocket did not match import path git.xxx.com/xx/go-lib-websocket)
迁移到 git module
- 历史版本管理工具
- dep
 - glide
 
 - 如何从历史版本管理工具迁移
go mod initcreates a newgo.modfile and automatically imports dependencies fromGodeps.json,Gopkg.lock, or a number of other supported formats.
 - 如何从内网git仓库迁移到类似github的标准仓库
- 必须加域名,否则代码里充斥了很多 IP
 - 加了域名,相当于包名都改了
 - 所有引用它的包都要改引用方式,改动量非常大
 - 临时方案
- 在本地用 replace 方式引用
 - 改动本地的库的名字,以及库里代码的引用名字
 
 
 - remote import paths 如何工作
- go - Remote import paths
- When using GOPATH, downloaded packages are written to the first directory listed in the GOPATH environment variable. (See ‘go help gopath-get’ and ‘go help gopath’.)
 - When using modules, downloaded packages are stored in the module cache. (See ‘go help module-get’ and ‘go help goproxy’.)
 
 
 - go - Remote import paths
 - 最终方案: