Go Modules, the official Go language dependency management tool, has been released for a long time. From 1.14 dependency, the support of Go Modules is automatically opened by default. It is believed that many people in the company’s projects have switched from Go Vendor, DEP and other dependency management to Go Modules.

Go Modules claims to be a language native, zero configuration package dependency management tool out of the box, of course zero configuration is only true if our project relies on external public packages that don’t conflict with each other. In my previous article, “How to Use GoModules to manage Your dependencies,” I summarized some of the steps you can take to switch to GoModules for package dependency management, as well as how to use the replace directive to overcome the perplexing incompatibility between ETCD and GRPC versions.

However, I’ve noticed that some of my friends have problems with how to get Go Modules to use a private repository package as a dependency module when they first switch to Go Modules. This article summarizes the two things you need to configure to make Go Modules use a private repository.

Configure Git to pull private dependencies using SSH

If nothing is set, the following error occurs when our project references a private repository package via Go Modules:

Go: downloading code.lazycorp.com/privaterepo v0.0.0-20200408100711 - ed766a2975ce go get code.lazycorp.com/privaterepo: code.lazycorp.com/[email protected]: verifying the module: code.lazycorp.com/[email protected]: Reading https://sum.golang.org/lookup/code.lazycorp.com/[email protected]: 410 Gone server response: not found: code.lazycorp.com/[email protected]: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /tmp/gopath/pkg/mod/cache/vcs/13e63a509893edc19353a80fa2c6e28db213d146f72fe43ba65c1ec86624027b: exit status 128: fatal: could not read Username for 'https://code.lazycorp.com': terminal prompts disabledCopy the code

The reason for this problem is that the Go Moduels mode downloads code from the Git repository over HTTPs, and we want (instead of using HTTPs) to download these private dependencies over SSH so that we can access the Git repository using a locally set SSH key.

What we can do to solve this problem is to configure Git to rewrite all HTTPs urls to the equivalent Git SSH URL:

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

Copy the code

Code.lazycorp.com is a name I made up randomly. This should be replaced with the domain name of your company’s Git repository, or if you are using a private repository on GitHub:

git config --add --global url."[email protected]:".insteadOf https://github.com
Copy the code

Configure the GOPRIVATE environment variable

This environment variable is intended for private dependencies and is typically set to the domain name of the repository site. Again, if the domain name of the company’s internal repository site is code.lazycorp.com, then we will set this environment variable to

export GOPRIVATE=code.lazycorp.com
Copy the code

With this setting, all modules whose path prefix is code.lazycorp.com will no longer go through the mirror site specified by GOPROXY to pull the corresponding module package, but instead will go to code.lazycorp.com to pull the corresponding module package.

Here’s a tip: if you don’t want to contaminate the global environment variables on your computer while developing, you can choose to enable Go Modules support in GoLand and set the environment variables so that you can use Go Modules when building and running programs in GoLand.

conclusion

After the above two steps of configuration can smoothly let Go Modules use private dependency, if there are Go Modules use problems, welcome to leave a comment to discuss, I believe there are many hidden in the reader can solve these problems together.

If you like my article, please give me a thumbs-up. I will share what I have learned, seen and practiced first-hand through technical articles every week. Thank you for your support. Wechat search concern public number – network management bi bi bi weekly to teach you an advanced knowledge.