Go Modules is a new feature in Golang 1.11. Now that 1.13 is in its seventh minor release, it has been used by almost all major projects, including many in the Kubernetes ecosystem. When I was developing OAM related projects, I found that the functions of Modules seem simple, but they are not so easy to use. Therefore, I want to share with you my experience in using modules, hoping that you can learn how to use modules in the shortest time to avoid stepping on pits.

What is modules?

Golang’s package management is notoriously chaotic. It used to rely on $GOPATH, as long as your code is placed in the specified path. There is no “package management” concept. After being teased by the community for a long time, I started to build vendor mechanism. In simple terms, codes can be placed not only in the specified path, but also in the vendor folder of the project’s own path. The problem with this is that the upstream changes you refer to in the code package don’t directly affect your project, which is obviously starting to concern yourself with the “package version.” Unfortunately, the problem of package management is still not solved. For example, what if different packages depend on different versions of the same package? What about code conflicts between versions? The vendor mechanism was not solved, so dozens of package management tools were created around vendor/ community. All of a sudden, a hundred flowers bloom, a hundred schools of thought contend, and each has its own strengths, leading to some confusion in Golang’s package management ecology. If you are interested in this history, you can read my article on Go Package Management.

More interesting is that after seeing the mess of package management tools in the go official community, we also made a tool with similar functions deP. The principle is similar to other kinds of vendor-dependent package management tools, and we are ready to unify package management. When people have expectations for DEP tool and start to switch to DEP tool to manage dependency packages, go officially released the current modules mechanism, completely abandoning the previous DEP tool and vendor mechanism. Such operation has caused a huge controversy in the community. Modules is well integrated with go Get, Go Build and other official tools. The official intention is naturally to throw away the original historical burden and save the world in a new way. However, the actual experience is still unsatisfactory.

The general trend has been toward modules, and go1.13 has done a lot of work with modules.

Without further ado, the goal of this article is to walk you through Go Modules in 5 to 10 minutes and then describe some common problems in QA form. You can also refer to the official documentation for more details.

Initialize the

The modules mechanism’s subcommand in Go is go mod.

Modules was introduced in Go 1.11, so check your go version before you start using it. It is recommended to use the latest version 1.13, which includes many updates and features related to the Module mechanism.

After ensuring that go is at least 1.11 or later, you need to turn on the environment variable GO111MODULE to enable the Module mechanism.

export GO111MODULE=onCopy the code

Or set to Auto mode so that projects in the GOPATH path do not use the Module mechanism.

export GO111MODULE=autoCopy the code

You are advised to add it to ~/. Bashrc or ~/. ZSHRC.

If your project was previously managed with Modules, your local environment is already initialized by this point.

If modules is not already used in your project, it’s easy to switch to modules by deleting the original vendor folder (you can move it outside the project to be safe) and performing an initialization command in your project.

Go mod init [Module name]Copy the code

The package name is still strongly associated with the Go Path as before, for example, our project is usually at http://github.com/oam-dev/oam-go-sdk, so your package name is this.

go mod init github.com/oam-dev/oam-go-sdkCopy the code

Once the initialization is complete, you should see a go.mod file in the project.

You can then download all the dependencies from the original Vendor by going to Go Mod Download.

Daily Package Management

With go Module, many of your commands are integrated with package management. Go Get, Go Build, and Go Run all automatically find and update dependencies in go.mod.

So according to the Go team, in general, you don’t have to worry about package management at all. (Of course, this is purely official.)

So you’ve got it by nowgo modulesIt’s been, like, five minutes, right?

FAQ

In fact, because Go officials put too much emphasis on package management too late, packages don’t have any concept of versions, so conflicts can happen, and because Go Modules are so automatic, even if you learn how to use modules, you still end up with a headache.

Here we answer all kinds of questions in the project in the form of FAQ.

What if some packages are inaccessible for specific network reasons?

The Go Module adds a proxy mechanism to provide proxy access by setting a proxy address. Ali Cloud provides such a go agent, is a completely free service. The common proxy repository proxies and caches go modules, which you can use to avoid slow or failed module pulls due to DNS contamination or other issues and speed up your project build.

The setting method is very simple, just need to set the following environment variables, run the command:

export GOPROXY=https://mirrors.aliyun.com/goproxy/Copy the code

GOPROXY: mirror GOPROXY: mirror GOPROXY: mirror GOPROXY: mirror GOPROXY: mirror GOPROXY: mirror GOPROXY

go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,directCopy the code

A comma can be used to add multiple proxies, and a direct is used to access private repositories that cannot be accessed by proxies.

This feature is almost a must-have tool for telecommuting.

The company has built a private library through GitLab. What if the two parties can’t download the library?

This is almost the most common problem, and a simple solution is to hack git configuration:

git config --global url."[email protected]:<your>/<package>.git".insteadOf "https://gitlab.your-company.com/<your>/<package>.git"Copy the code

This scheme relies on your local ~/.ssh/id_rsa so that you can go get normally.

How to solve the dependency package problem of private library in Dockerfile building image?

  • Solution 1: The above method is modifiedgit configDepending on your local~/.ssh/id_rsa, can be passed at build timemultistage-buildAdd the private key tostage 0Build, then build the image with the new stage, so that the image does not contain the private key;
  • Option 2: A safer way is to use the Docker image locally before building it each timego mod vendorThe package is cached and dependencies are managed by GOPATH and Vendor during the Dockerfile image building process.

How does a dependency package update a given version?

First check the version:

IO /sampler Rsc. IO/Sampler v1.0.0 v1.2.0 v1.2.1 v1.3.0 v1.3.1 v1.99.99Copy the code

Update:

$go get Rsc. IO /[email protected] go: Finding Rsc. IO /sampler v1.3.1 GO: Downloading rsc. IO /sampler v1.3.1 go: Rsc. IO/Sampler V1.3.1 $GOtestPASS ok example.com/hello 0.022 sCopy the code

What if some dependent packages cannot be found due to address changes?

Go’s dependencies are directly related to the project name, which means that if we use a project on Github and the project’s maintainer changes the project name on a whim, all projects that rely on it will be unable to find dependencies.

Thankfully, there’s a replace mechanic:

The replace golang.org/x/crypto v0.0.0-20190313024323-a1 f597ede03a = > github.com/golang/crypto v0.0.0-20190313024323-a1f597ede03aCopy the code

What about version conflicts?

This is about to comb the version, is the most no shortcut. An easy way to do this is to delete all packages in Go.mod that do not need to be specified, specify only the necessary package versions, and then use Go Build to have the project automatically build dependent packages.

The go Mod Graph shows the dependency paths:

$go mod graph github.com/oam-dev/oam-go-sdk github.com/go-logr/[email protected] github.com/oam-dev/oam-go-sdk github.com/onsi/[email protected] github.com/oam-dev/oam-go-sdk github.com/onsi/[email protected] github.com/oam-dev/oam-go-sdk github.com/stretchr/[email protected] github.com/oam-dev/oam-go-sdk golang.org/x/[email protected] Github.com/oam-dev/oam-go-sdk k8s. IO/[email protected] github.com/oam-dev/oam-go-sdk k8s. IO/[email protected] Github.com/oam-dev/oam-go-sdk k8s. IO/[email protected] github.com/oam-dev/oam-go-sdk Sigs. K8s. IO/[email protected]...Copy the code

On the left are the project packages, and on the right are the dependent packages and versions.

If you do have two conflicting packages that need to be versions specified, it’s time to make a trade-off, change the code, upgrade or downgrade a package.

How are local packages referenced?

If you need to modify other dependent project code during code debugging, you can refer to the local package, or you can use replace:

Require (golang.org/x/crypto v0.0.0) replace golang.org/x/crypto v0.0.0 =>.. /cryptoCopy the code

The latter is the path where the local dependencies are relative to the project path.

With these problems solved, you should basically be happy to use the Module function.

Go mod Command overview

There are a few other features in the Go Mod that you can check out here:

The last

OAM (Open Application Model) is a Model for cloud native applications jointly developed by Alibaba and Microsoft. It is the first time that the “application-centered” infrastructure and construction specifications are completely described. Application managers can write a self-contained, self-described “application definition file” as long as they follow the specification.

OAM application will be divided into two parts of the application components and application characteristics, application component is the logic of the application itself, and the application characteristics is the cloud of general ability (such as enlarge shrinks capacity, monitoring, gray, etc.), greatly improved the application build-time modular reuse, turn the cloud all kinds of resources and capabilities in order to standardize object “declaration”.

At the same time, OAM emphasizes separation of concerns and layering APIS at different stages of application development through standardized models. In the process, application components are defined by R & D first, and then various strategies on the cloud are configured by operation and maintenance. Finally, the infrastructure team provides various modular capabilities uniformly. OAM acts as the glue for collaboration, greatly improving the efficiency of application delivery.

OAM related content is completely open source on Github, and we also wrote the OAM-Go-SDK for Go ecology to facilitate the rapid implementation of OAM.

At present, Alibaba team is contributing and maintaining this set of technology in the upstream. If you have any questions or feedback, you are very welcome to contact us in the upstream or Dingding.


Read more: https://yq.aliyun.com/articles/744783?utm_content=g_1000104490

On the cloud to see yunqi: more cloud information, on the cloud case, best practices, product introduction, visit: https://yqh.aliyun.com/