Pip 爽惯了到了 Go 是真的头疼,于是要彻底了解一下
前言
- 在python中,一时
pip install一时爽,一直install一直爽 - 在Java中也有maven和gradle等优秀成熟的依赖版本管理工具
- 虽然从go1.12开始接触go语言(go module在>=go1.11中存在),因此也没有经历过那段艰难的时刻,但是还是对于其包管理很迷茫
- 从
go get到go dep到go mod - 官方文档
使用
Get Start
在默认情况下,$GOPATH 并不支持go modules,因此要通过如下命令将go modules开启
1
export GO111MODULE=on
使用
go mod init初始化.mod文件使用
go mod tidy安装缺失的依赖库并会自动去除无用的依赖然后更新依赖到当前版本
一些命令
1 | go mod init # 初始化.mod文件 |
问题来了
出现
verifying xxx的报错,导致依赖下载不下来怎么办?GOPROXY你值得拥有
Go 1.11在支持go module的同时也加入了GOPROXY环境变量,为官方依赖源无法下载的包提供第三方依赖代理可通过一下方式对下载代理进行配置
1
2
3
4export http_proxy=http://proxyAddress:port
export https_proxy=http://proxyAddress:port
或
export all_proxy=http://proxyAddress:port为避免每次都是用上述方法,可将配置放入
profile中,在每次启动bash终端时会自动运行1
2
3# ~/.bash_profile
export http_proxy=http://proxyAddress:port
export https_proxy=http://proxyAddress:port
补充知识
- bash_profile vs bashrc
.bash_profile会在login shell启动时自动执行.bashrc会在bash启动的shell打开前自动执行
- login shell & interactive shell & non-interactive shell
- login shell是当你通过键入身份信息/ssh方式登入的shell窗口,其依次会执行
.profile.bash_profile.bash_login
- interactive shell 当你使用如
bash就会激活ineractive shell - non-interactive shell 当你使用如
sh运行脚本
- login shell是当你通过键入身份信息/ssh方式登入的shell窗口,其依次会执行