Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to learn to use go modules

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to learn to use go modules, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.

Go modules are a new feature added to Golang 1.11. Now that 1.13 has released its seventh minor version, almost all major projects have started to use, which naturally includes many projects in the Kubernetes ecosystem. When I was developing OAM related projects, I found that the functions of modules seemed simple, but they were not so easy to use, so I wanted to share my experience with you.

What are modules?

Simply put, it is package management. Golang's package management has always been known for its chaos. In the past, it relied on $GOPATH. As long as your code is placed under the specified path, there is no concept of "package management" at all. After being criticized by the community for a long time, I began to work on the vendor mechanism. In short, the code can not only be placed in the specified path, but also in the vendor folder of the project's own path. This solves the problem that upstream changes to the code package you reference do not directly affect your project, which is obviously starting to care about "package versions." Unfortunately, there are still package management issues that haven't been resolved, such as how different packages depend on different versions of the same package? What about code conflicts between versions? The vendor mechanism has not been solved, so dozens of package management tools have emerged around the vendor/community. At one time, a hundred flowers bloom, a hundred schools of thought contend, and each has its own strengths, resulting in some confusion in the package management ecology of golang. Those interested in this history can read the article "Go Package Management's Past and Present Life" written by the author.

More interestingly, after seeing the chaos of package management tools in the go official community, they also made a tool dep with similar functions. The principle is similar to other package management tools that rely on vendor/mechanism, and they are ready to unify package management. When everyone expected the dep tool and started switching to the dep tool management dependency package, go officially released the current modules mechanism, completely abandoning the previous dep tool and vendor mechanism. Such operations have caused great controversy in the community. Modules are well integrated with official tool ecosystems such as go get and go build. The official intention is naturally to put aside the original historical baggage and save the world in a brand-new way. However, the actual experience was still unsatisfactory.

In general, the general trend has been to use modules, and go1.13 has also done a lot of work on the modules mechanism.

Anyway, the goal of this article is to teach you how to use go modules in 5 - 10 minutes and then describe some common problems in QA form. If you want to understand the relevant content in detail, you can also refer to the official documentation.

initialization

modules The subcommand in go is go mod.

Modules mechanism is only introduced in go 1.11, so check your own go version before starting to use it. It is recommended to use the latest version 1.13, which covers more updates and functions related to module mechanism.

After ensuring that the go version is at least 1.11 or above, turn on the environment variable GO111MODULE to enable the module mechanism.

export GO111MODULE=on

Or set it to auto mode, so that projects in GOPATH don't use module mechanism.

export GO111MODULE=auto

It is recommended to add it to ~/.bashrc,~/.zshrc and other configurations to automatically take effect.

If your project was previously managed with modules, your local environment is now initialized.

If you haven't used modules before in your project, switching over is easy. Delete the original vendor folder (you can move it outside the project to be safe) and execute the initialization command in your project.

go mod init [module name]

The package name is the same as before, still strongly associated with go path. For example, our project is generally http://github.com/oam-dev/oam-go-sdk, so your package name is this.

go mod init github.com/oam-dev/oam-go-sdk

After initialization, you will see a go.mod file in your project.

Then go mod download to download all dependencies from the original vendor.

Daily package management

After using go module, many of your commands will be integrated with package management, such as go get, go build, go run will automatically find and update dependencies in go. mod.

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

So you've learned to go modules by now, not more than five minutes, right?

FAQ

In fact, because go officials paid too much attention to package management, all kinds of packages did not have the concept of version, and there would be various conflicts casually, and because go modules were too automatic, even if you learned how to use modules, you would still have a headache in the end.

Below we answer questions in the form of FAQ.

What if some packages are inaccessible due to specific network reasons?

Go module adds proxy mechanism, as long as you set a proxy address, you can provide proxy access. Alibaba Cloud provides such a go agent, which is a completely free service. The public proxy repository proxies and caches go modules, and you can use this proxy to avoid DNS pollution or other problems causing slow or failed module pull and speed up your project construction.

The way to set it is very simple. You only need to set the following environment variables and execute the command:

export GOPROXY=https://mirrors.aliyun.com/goproxy/

go1.13 added mirror mechanism, you can set mirror through go env -w, in fact, is the previous GOPROXY do more in place, execute the command:

go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

Multiple proxies can be added after the comma, and the final direct is to directly access when all proxies are not found. Private repositories that cannot be accessed by proxies can be used normally.

This feature is basically a must-have tool for telecommuting from home.

The company built a private library through gitlab. What if the two-party dependency library cannot be downloaded?

This is almost the most common problem, the simpler solution is to hack the git configuration:

git config --global url. "git@gitlab.your-company.com:/.git".insteadOf "https://gitlab.your-company.com//.git"

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

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

Solution 1: The above method modifies git config, but relies on your local ~/.ssh/id_rsa. During construction, you can add the private key to stage 0 through multistage-build, and then use the new stage to generate an image. In this way, the image will not contain the private key.

Option 2: A safer way is to cache packages locally with go mod vendor before each Docker image is built, and use GOPATH and Vendor mechanisms to manage dependencies during Dockerfile image building.

How does a dependency package update a specified version?

Check the version first:

$ go list -m -versions rsc.io/samplerrsc.io/sampler v1.0.0 v1.2.0 v1.2.1 v1.3.0 v1.3.1 v1.99.99

Further update:

$ go get rsc.io/sampler@v1.3.1go: finding rsc.io/sampler v1.3.1go: downloading rsc.io/sampler v1.3.1go: extracting rsc.io/sampler v1.3.1$ go testPASSok example.com/hello 0.022s Some dependent packages cannot be found due to address changes What should I do?

The dependencies of go are directly related to the project name, so if we use a project on github and the project maintainer suddenly changes the project name, all projects that depend on it will not find dependencies.

Fortunately, there is a replacement mechanism:

replace golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a => github.com/golang/crypto v0.0.0-20190313024323-a1f597ede03a version conflict how to do?

This is about sorting out the version, and there is no shortcut. A simpler approach is to delete all packages that don't require a specific version from go.mod, specify only the necessary package versions, and then let the project automatically build the dependent package versions via go build.

You can view the dependency path by going mod graph:

$ go mod graphgithub.com/oam-dev/oam-go-sdk github.com/go-logr/logr@v0.1.0github.com/oam-dev/oam-go-sdk github.com/onsi/ginkgo@v1.10.1github.com/oam-dev/oam-go-sdk github.com/onsi/gomega@v1.7.0github.com/oam-dev/oam-go-sdk github.com/stretchr/testify@v1.4.0github.com/oam-dev/oam-go-sdk golang.org/x/net@v0.0.0-20191004110552-13f9640d40b9github.com/oam-dev/oam-go-sdk k8s.io/api@v0.17.0github.com/oam-dev/oam-go-sdk k8s.io/apimachinery@v0.17.0github.com/oam-dev/oam-go-sdk k8s.io/client-go@v0.17.0github.com/oam-dev/oam-go-sdk sigs.k8s.io/controller-runtime@v0.4.0...

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

If there are two conflicting packages that require a specific version, a trade-off is made, code changes are made, and a package is promoted or demoted.

How are native packages referenced?

If code debugging involves modifying code from other dependent projects, then you need to reference native packages, or you can use the replace mechanism:

require ( golang.org/x/crypto v0.0.0)replace golang.org/x/crypto v0.0.0 => ../ crypto

This latter is the path of a local dependency relative to the project path.

After solving these problems, you can basically use the module function happily.

Go mod command list

There are some other functions in go mod, which are also listed here for your convenience:

The OAM (Open Application Model) open application model is a model for cloud native applications jointly developed by Alibaba and Microsoft. For the first time, the "application-centric" infrastructure and construction specifications are fully elaborated. Application managers can write a self-contained, self-describing "application definition file" as long as they follow this specification.

OAM divides the application into two parts: application component and application feature. Application component is the logic of application itself, while application feature is various general capabilities on cloud (such as expansion and contraction, monitoring, gray scale, etc.), which greatly improves the modular multiplexing ability during application construction and transforms all kinds of resources and capabilities on cloud into standardized "declarable" objects.

At the same time, OAM emphasizes separation of concerns, layering APIs at different stages of application development through standardized models. In terms of process flow, application components are defined by R & D, various policies on the cloud are configured by operation and maintenance, and finally various modular capabilities are uniformly provided by infrastructure teams. OAM acts as a cohesive agent for each other, greatly increasing the efficiency of application delivery.

O

About how to learn to use go modules questions to share here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report