Perfectly solve the problem of golang go get private warehouse

Time:2021-9-13

Solve the problem of golang go get gitlab private warehouse (1.13)

1. Problem description


require ( 
 git.xxxxxxx.com/middle/user v0.0.1
)

Go mod tidy import Package Failed

Go get git.xxxxxxxx.com/middle/user failed

Go build has a checksum process and cannot be compiled

2. Phenomenon analysis

Go get does not support warehouses other than code support. And the GIT call chain process adopts HTTPS

During the download process, if goproxy is set on the machine, the download will fail

The compilation process will cause checksum to fail

3. Materials

material explain
git.xxxxxxx.com Private warehouse
middle/user.git User service module

programme

1. Tag the released warehouse, such as v0.0.1, so that the warehouse address can be identified

2、export GOPRIVATE=git.xxxxxxx.com

When go build, the system will not use goproxy and will no longer verify sum

3. Adjust git HTTPS = = = > SSH, and note that username is changed to your own username


[url "[email protected]"]
    insteadOf = https://git.xxxxxxx.com

conclusion

This problem, Google can be called a unique, capricious enough

Add: go get pulls private items and encounters a 404 not found solution

problem

When using go module for package management, to obtain the latest package from the remote warehouse, use go get + project name to obtain it, prompting 404 not found, as shown in the figure:

在这里插入图片描述

Causes and Solutions

This is because go get failed because it did not specify the user and password when obtaining the remote package, resulting in no permission

way

Add the “- insecure” parameter when go get, as shown in the figure:

在这里插入图片描述

Supplement: golang configuration private warehouse

Configure a repository that uses SSH access

1. When go mod pulls the dependent library according to go.mod

Will use HTTPS. For convenience, we can also use SSH to pull dependencies by configuring git global configuration. The following is how to configure HTTPS to SSH:


git config --global url."[email protected]:".insteadOf https://gitee.com/

2. Configure environment variables

To specify a private warehouse, which is used in a proxy free manner


go env -w GOPRIVATE=gitee.com

The private warehouse configured here is gitee

3. Set up agent


go env -w GOPROXY=goproyx.io

Common errors:

1. Error 1

[email protected] helloworld % go get -u gitee.com/abc/helloworld/v3 go: gitee.com/abc/helloworld/[email protected] requires gitee.com/abc/[email protected]: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /Users/abc/developer/golang/pkg/mod/cache/vcs/742008abb4987f237c93efc5ddde7db6dd8d1841fe94aea076046d86a92e26a7: exit status 128: fatal: could not read Username for ‘https://gitee.com’: terminal prompts disabled

This error is that HTTPS without Git is converted to SSH

2. Error 2

go: gitee.com/abc/[email protected] requires gitee.com/abc/[email protected]/go.mod: verifying module: gitee.com/abc/[email protected]/go.mod: reading https://goproxy.io/sumdb/sum.golang.org/lookup/gitee.com/abc/[email protected]: 410 Gone server response: not found: gitee.com/abc/hellowor[email protected]: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /tmp/gopath/pkg/mod/cache/vcs/742008abb4987f237c93efc5ddde7db6dd8d1841fe94aea076046d86a92e26a7: exit status 128: fatal: could not read Username for ‘https://gitee.com’: terminal prompts disabled

This error is the goprivate setting error, which causes go to verify the sum of the library

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.