go mod 默认使用https
go mod 默认使用https作为代码管理的交互协议。其原因:其一,为了保证访问的兼容性,采用http通用性最好;其二,https也能有效的保证传输安全。
以下是go凯发k8官网下载客户端中心官网中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]"
阅读(1067) | 评论(0) | 转发(0) |