包管理是 Go 一直被詬病做的不好的功能。在 1.11 之前,go get 缺乏對依賴包版本管理和 reproducible build 的支持。當時在 Go 社區當時誕生了許多好用的工具,比如 glide,dep 等。在 1.11 版本之后, Go 引入了 Go Module,再也沒有 GOPATH 的限制,你可以隨意在任何路徑寫項目,但是此時對私有倉庫的支持還不是很好。而在 1.13 版本之后, Go 對 Go Module 又進行了優化,支持了复制GOPRIVATE環境變量,可以指定私有倉庫的地址,使用十分便捷。大家在使用過程中,或多或少地會遇到一些問題,下面我針對自己遇到的問題進行總結。
go get
如果在沒有進行任何設置的情況下直接執行复制go get your.gitlab.com/pkg/example,你很可能會遇到以下錯誤:
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:
复制machinegithub.comloginUSERNAMEpasswordAPIKEY
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:
評論