go mod 默认使用https
go mod 默认使用https作为代码管理的交互协议。其原因:其一,为了保证访问的兼容性,采用http通用性最好;其二,https也能有效的保证传输安全。
以下是go官网中A&Q中,对于该问题的回答:
-
Why does "go get" use HTTPS when cloning a repository?
-
Companies often permit outgoing traffic only on the standard TCP ports 80 (HTTP) and 443 (HTTPS), blocking outgoing traffic on other ports, including TCP port 9418 (git) and TCP port 22 (SSH). When using HTTPS instead of HTTP, git enforces certificate validation by default, providing protection against man-in-the-middle, eavesdropping and tampering attacks. The go get command therefore uses HTTPS for safety.
-
-
Git can be configured to authenticate over HTTPS or to use SSH in place of HTTPS. To authenticate over HTTPS, you can add a line to the $HOME/.netrc file that git consults:
-
-
machine github.com login USERNAME password APIKEY
-
For GitHub accounts, the password can be a personal access token.
-
-
Git can also be configured to use SSH in place of HTTPS for URLs matching a given prefix. For example, to use SSH for all GitHub access, add these lines to your ~/.gitconfig:
-
-
[url "ssh://git@github.com/"]
-
insteadOf = https://github.com/
但是,该行为在使用私有仓库作为模块管理时(公司内部联合开发,必然有此需求),go get通常是不能够正确下载源码的。原因是,通常私有库在git访问时,都需要登录。
解决方法
其一,采用通过git 配置,让访问私有库的git请求,自动带上用户名和密码
-
其核心
-
git clone http://[username]:[password]@[your-repo].gitlab.com/[group]/[project].git
其二,在github/gitlab上配置 Personal Access Token,并且在本地git 配置中添加配置,用http访问时,自动添加token
-
[color]
-
ui = auto
-
[alias]
-
st = status
-
co = checkout
-
ci = commit
-
br = branch
-
[core]
-
editor = vim
-
-
[http]
-
extraheader = PRIVATE-TOKEN: gGXLa1j8JpPxjjMvY5X7
其三,使用ssh协议替代http[s]协议
-
git config --global url."git@[your-repo]".insteadOf "https://[your-repo]"
通常,公司有自己的gitlab服务器,每个人都上传了ssh key的,因此更推荐使用ssh协议替换https协议作为内部go 模块的交互协议。
特别提醒
在国内,github通常存在访问问题,因此,go开发者都需要配置proxy来下载包。当设置了proxy之后,对于采用自建的代码服务需要避开代理,否则无法正常访问。新版本go支持一个环境变量GOPRIVATE 来避免该问题。设置如下:
-
go env -w GOPRIVATE="*[your-repo]"
阅读(1092) | 评论(0) | 转发(0) |