Golang Team released version 1.12 in late February, while Athens released version v0.3.0 in early March, a new go language package management ecosystem in the making ** *

Recently, I was forced to die by Glide. When I downloaded the package outside the wall, it was like The six pulse Sword of Duanyu. At that time, go 1.12 was released and the Athens 0.3.0 update was released by Microsoft go Module agent. In particular, I have done usability research on go Module and Athens.

01

go module

Go 1.11 introduced the concept of Go Module on a trial basis and initially solved the problem of go language package management. Talk is cheap, show me the code.

Let’s create a new project, Test, which creates a GRPC client and makes a simple RPC call

$ mkdir test && cd test
Copy the code
package main
Copy the code

Using the go mod init test command, a go.mod file is created under the current directory

# philchia @ philchia in ~/test [22:36:31]
Copy the code

Go.mod declares the name of the module and the version of Go used when the current module was built

The google.golang.org/grpc package is usually not directly downloadable for some reason, Here we use the Go Module’s Download Protocol to get our dependencies through a proxy by setting GOPROXY=https://goproxy.io

$ GO111MODULE=on GOPROXY=https://goproxy.io go get google.golang.org/grpc
Copy the code
Go: finding google.golang.org/grpc v1.19.0Copy the code

Now that our dependencies are all downloaded, we can see that a new go.sum file has been added, and the contents of the go.mod file have been changed

$ cat go.mod 
Copy the code

Try compiling our main.go

$ go build main.go
Copy the code

bingo! Compile successfully

02

athens

IO public Go Module proxy can solve the problem of downloading dependencies outside the wall. However, when our dependencies are stored in the private library of gitLab, ordinary Go Module proxy is not able to solve the problem. Athens is an open source Go Module proxy service with Microsoft participation and contributed code.

Athens implements the Go Module Download Protocol and supports fetching packages from private libraries. With Docker, you can easily create an Athens proxy server.

Start by creating two directories, one for.netrc files and one for the Go Module agent

$ pwd
Copy the code

Create a.netrc file and write the address and username and password of our private library

cd athens-netrc
Copy the code

Start your Athens container

docker run -d -v /Users/goheart/athens-netrc:/root -v /Users/goheart/athens-storage:/var/lib/athens  -e ATHENS_NETRC_PATH=/root/.netrc -e ATHENS_DISK_STORAGE_ROOT=/var/lib/athens    -e ATHENS_STORAGE_TYPE=disk    --name athens-proxy    --restart always    -p 3000:3000    gomods/athens:latest
Copy the code

Visit http://localhost:3000 to check whether the container started successfully

curl http://localhost:3000
Copy the code

Configure to use our local Athens agent

$ export GOPROXY=http://locaohost:3000
Copy the code

This allows us to use proxy servers to import private libraries that rely on One More Thing. If you fork code on Github without using the Go Module, the import path in your own branch code will sometimes have the path of the original code base. With gomodule, the import path of our code does not need to include hostname and Github username, so we can support fork perfectly

Source: The Heart of Go

Focus and love cutting-edge & interesting development technology, join ** * Hello World