In the previous article, we introduced Go Modules. The Go Module supports Versioned Go and initially solves the problem of package dependency management.

There are also some problems with the new working mode. In the mainland, we can’t get some third-party packages directly through the Go get command. The most common one is the excellent packages at golang.org/x. Once working in a module, go Build will no longer care about the package under GOPATH or Vendor. Instead, go build will go to GOPATH/ PKG /mod to check whether there is a cache. If not, it will download a certain version of module. They often fail on the mainland. This article will focus on the proxy configuration of the Go Module, including the following two proxy configurations:

  • GOPROXY
  • Athens

GOPROXY

Goproxy is an open source project. When a user requests a dependent library, if it finds that the code is not locally available, it automatically requests the source and then caches it locally, allowing the user to request data from Goproxy.io. Of course, this is all done in one request. Goproxy. IO supports only go Module mode. $GOPROXY//@v/list = $GOPROXY//@v/list = $GOPROXY//@v/list $GOPROXY//@v/.info, $GOPROXY//@v/.mod, $GOPROXY//@v/.zip, etc.

By command:

export GOPROXY=https://goproxy.io
Copy the code

Once the environment variable is set, the go command will interact with the Proxy to download a specific version of the Module through the Go Module Download Protocol. Of course, we can also close the GOPROXY proxy by emptying the GOPROXY variable.

See: github.com/goproxyio/g…

Athens

Dependencies are immutable blocks of code and associated metadata from Github. They’re stored in a athens-controlled warehouse.

You probably already know what “immutable” means, but let me point it out again because it is very important to the system as a whole. When people change their packages, iterate, experiment, or anything else, the Athens code doesn’t change. If the package author releases a new version, Athens will pull it down. For example, a project that relies on package M version V1.2.3 will never change the package on Athens.

The installation

Athens supports a variety of installation methods, including Docker containers, K8S, and binary packages. This article explains how to install your Athens using binary packages.

git clone https://github.com/gomods/athens
cd athens
make build-ver VERSION="0.2.0"
Copy the code
 ./athens -version
Copy the code

Obtaining a private repository

Athens gets modules from a private repository, which is also an enterprise-level requirement. Generally, enterprise private repositories require authentication, so you need to configure the account and credential information to access the private repository in Athens. The Athens official documentation currently provides the ability to access a private repository with authentication through.netrc.

Private warehouse authentication is performed by creating a.netrc file.

//.netrc

machine github.com
  login MY_USERNAME
  password MY_PASSWORD
Copy the code

Local application

export GO111MODULE=on
exportGOPROXY = http://127.0.0.1:3000Copy the code

We can use the Walkthrough that Athens provides.

git clone https://github.com/athens-artifacts/walkthrough.git
Copy the code
$ cd. / Walkthrough $go run. go: Finding github.com/athens-artifacts/samplelib v1.0.0 handler: GET/github.com/athens-artifacts/samplelib/@v/v1.0.0.info [200] handler: GET/github.com/athens-artifacts/samplelib/@v/v1.0.0.mod [200] go: Downloading github.com/athens-artifacts/samplelib v1.0.0 handler: GET /github.com/athens-artifacts/samplelib/@v/v1.0.0.zip [200] The 🦁 says rawr!Copy the code

The output of go run. Includes an attempt to find the github.com/athens-artifacts/samplelib dependency. Because the agent is running in the background, you can also see output from Athens, indicating that it is processing requests for dependencies.

global public proxy

Athens also offers an experimental proxy:athens.azurefd.net for use by gopher worldwide. Import environment variables:

export GOPROXY="https://athens.azurefd.net"
Copy the code

Of course, you can use these public agents, but the source of the package is not complete. The safest approach is to build your own Athens service. The official issue says:

Athens.azurefd.net is in alpha and the infrastructure is not yet well maintained, so it is not surprising to see the timeout. Build your own Athens or use GoCenter (currently the only hosted Go module repository).

Recommended reading

Package management tools for Go

Subscribe to the latest articles, welcome to follow my official account

reference

  1. Athens docs
  2. Explore go Modules again: Use and details