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 understand go mod in Golang

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to understand go mod in Golang. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

What is go.mod?

Go.mod is a new official package management tool introduced in the Golang1.11 version to solve the problem that there is no place to record the specific version of the dependent package, so as to facilitate the management of dependent packages.

Go.mod is actually a Modules, and the official definition of Modules is:

Modules is a collection of related Go packages and a unit of source code exchange and version control. The go command directly supports the use of Modules, including recording and parsing dependencies on other modules. Modules replaces the old GOPATH-based method to specify which source files to use.

Unlike traditional GOPATH, Modules does not need to contain subdirectories such as src,bin, and a source code directory or even an empty directory can be used as a Modules, as long as it contains go.mod files.

How to use go.mod

First, upgrade the version of go to above 1.11.

Set up GO111MODULE

GO111MODULE

GO111MODULE has three values: off, on, and auto (the default).

The module feature will not be supported on the GO111MODULE=off,go command line, and the way to find dependent packages will be found through the vendor directory or GOPATH mode as in the old version.

The GO111MODULE=on,go command line uses modules and does not look in the GOPATH directory at all.

GO111MODULE=auto, the default value, the go command line will decide whether to enable the module feature based on the current directory. There are two situations in this case: the current directory is outside of GOPATH/src and it contains go.mod files; and the current file is under the directory that contains go.mod files.

Go mod command

Golang provides go mod commands to manage packages. Go mod has the following commands:

How go.mod is used in a project

First, we need to create a new project outside the GOPATH/src directory, or copy the old project outside the GOPATH/src directory.

Once the PS:go.mod file is created, its contents will be fully controlled by go toolchain. Go toolchain modifies and maintains go.mod files when various commands are executed, such as go get, go build, go mod, etc.

Go.mod provides four commands: module, require, replace and exclude

The module statement specifies the name (path) of the package

Dependency module specified by require statement

Replace statements can replace dependency modules

The exclude statement can ignore the dependency module

Here is a file that we have created for hello.go:

Package main import ("fmt") func main () {fmt.Println ("Hello, world!")}

Second, in the current directory, the command line runs the go mod init + module name initialization module

That is go mod init hello

After running, a go.mod file is generated in the current directory, which is a key file through which all subsequent packages are managed.

Official note: in addition to go.mod, the go command maintains a file called go.sum that contains the expected encrypted hash of the content of a specific module version

The go command uses the go.sum file to ensure that future downloads of these modules retrieve the same bits as the first download to ensure that the modules on which the project depends do not change unexpectedly, whether maliciously, accidentally, or otherwise. Both go.mod and go.sum should check in version control.

Go.sum does not require manual maintenance, so you don't have to pay too much attention.

Note: init is not required in subdirectories, and all dependencies in subdirectories are organized in the go.mod file of the root directory

Next, let's introduce a third-party package to our project.

If you modify the hello.go file as follows, as in the past, to run hello.go, you need to execute the go get command to download the gorose package to $GOPATH/src.

Package main import ("fmt"github.com/gohouse/gorose") func main () {fmt.Println ("Hello, world!")}

However, with the new package management, this is no longer necessary.

Direct go run hello.go

Just a moment... Go automatically looks for packages in the code, downloads dependencies, and writes specific dependencies and versions to go.mod and go.sum files, while go reports an error that is referenced but not actually used.

Look at go.mod and it looks like this:

Module test require (github.com/gohouse/gorose v1.0.5)

The require keyword is the reference, followed by the package, and finally v1.11.1 is the version number of the reference

Thus, a small example of creating a project using Go package management is complete.

The above is how to understand the go mod in Golang. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report