For more study notes and sample code visit: github.com/wenjianzhan…

Unresolved dependency issues with Go

  1. Different projects in the same environment use different versions of the same package
  2. Unable to manage dependencies on a particular version of a package

Vendor path

With the Go 1.5 release, the Vendor directory was added to solutions that rely on directory lookup in addition to GOPATH and GOROOT. Before Go 1.6, you had to manually set environment variables

Solution for finding dependent package paths:

  1. The vendor directory under the current package
  2. Look in the parent directory until you find the vendor directory under SRC
  3. Look for dependency packages under GOPATH
  4. Look in the GOROOT directory

Common dependency management tools

godep glide dep gomod

The author uses gomod to initialize project package management

When the project is not in GOPATH, execute directly:

go mod init
Copy the code

The project will report an error:

$ go mod init remote_package                                                            
go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'

Copy the code

Then you need to manually activate Modules:

$ export GO111MODULE=on
Copy the code

Normally, export GO111MODULE= ON needs to be configured in the environment variable

Sudo vim ~/. Base_profile // append export GO111MODULE=on // Enable go Modules; // GOPROXY environment variables can also be set // a free, reliable, always-on, CDN accelerated module proxy. Export GOPROXY=https://goproxy.cn // Do not forget to reload source ~/.bash_profile // run again (need to be in the project) go mod init // or specify the project path go mod init xxxCopy the code

After execution, a vendor folder and go.mod will appear in the project path

Vendor contains the dependency package

Go. Mod is the package reference information to store. You can use Go mod Tidy

Go Mod’s common CMD

The command instructions
download Download modules to local cache
edit Edit go.mod from tools or scripts
graph Print Module Requirement graph
init Initialize New Module in current directory
tidy Add missing and remove unused modules
vendor Make vendored copy of dependencies
verify Verify dependencies have expected Content
why Explain why packages or modules are needed

Why use GOPROXY

Because there are many modules in the Go ecosystem that Chinese Gopher cannot access, such as the most famous golang.org/x/… . It’s also a bit slow to get modules from GitHub in Mainland China. So we created Goproxy China to make it easier for Gopher in China to use the Go module. In fact, since goproxy.cn is already CDN accelerated, it can be used by Gopher in other countries.

For more study notes and sample code visit: github.com/wenjianzhan…