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 use go module

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use go module. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Go module using go module was launched after go version 1.14. Introduction to go module use

Go module is an official version management tool after the Go1.11 version, and starting from the Go1.13 version, go module will be the default dependency management tool for the Go language.

1.1 GO111MODULE

To enable go module support, the first step is to set the environment variable GO111MODULE, which enables you to turn module support on or off. It has three optional values: off, on, and auto. The default value is auto.

GO111MODULE=off disables module support and looks for packages in the GOPATH and vendor folders at compile time.

GO111MODULE=on enables module support, ignores the GOPATH and vendor folders when compiling, and downloads the dependencies to the% GOPATH%/pkg/mod/ directory based on the go.mod download dependency.

GO111MODULE=auto, enable module support when the project is outside $GOPATH/src and there are go.mod files in the project root directory.

To put it simply, after setting up GO111MODULE=on, you can use go module, so there is no need to create a project in GOPATH, and you can also manage the third-party package information that the project depends on.

Using go module to manage dependencies generates two files, go.mod and go.sum, in the root directory of the project.

1.2 GOPROXY

After Go1.11, set the GOPROXY command to:

Export GOPROXY= https://goproxy.cn

After Go1.13, the default value of GOPROXY is https://proxy.golang.org, which is inaccessible in China, so it is highly recommended that you set up GOPROXY. Here I recommend using goproxy.cn.

Go env-w GOPROXY= https://goproxy.cn,direct1.3 go mod command

Common go mod commands are as follows:

Go mod download download dependent module to local cache (default is $GOPATH/pkg/mod directory) go mod edit edit go.mod file go mod graph print module relies on figure go mod init to initialize the current folder, create go.mod file go mod tidy to add the missing module Delete useless modulego mod vendor will rely on copying to vendor go mod verify check depends on go mod why to explain why you need to rely on 1.4 go.mod

The go.mod file records all the dependency information of the project, and its structure is roughly as follows:

Module github.com/Q1mi/studygo/bloggergo 1.12require (github.com/DeanThompson/ginpprof v0.0.0-20190408063150-3be636683586 github.com/gin-gonic/gin v1.4.0 github.com/go-sql-driver/mysql v1.4.1 github.com/jmoiron/sqlx v1.2.0 github.com/satori/go.uuid v1.2.0 google.golang.org/appengine v1.6.1 / / indirect)

Among them

Module is used to define the package name

Require is used to define dependent packages and versions

Indirect stands for indirect reference

1.4.1 dependent version

Go mod supports semantic version numbers, such as go get foo@v1.2.3, and can also submit hashes to git branches or tag, such as go get foo@master, or to git, such as go get foo@e3702bed2. The dependent version supports the following formats:

Gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7gopkg.in/vmihailenco/msgpack.v2 v2.9.1gopkg.in/yaml.v2 github.com/golang/crypto v0.0.0-20180820150726-614d502a4dac golang.org/x/net v0.0.0-20180821023952-922f4815f713 = > github.com/golang/net v0.0.0-20180826012351-8a410e7b638d golang.org/x/text v0.3.0 = > github.com/golang/text v0.3.0) 1.5 go get

Execute the go get command in the project to download the dependency package, and you can also specify the version to download.

Running go get-u will upgrade to the latest minor version or revision (x.y.z, z is the revision number, y is the minor version number)

Running go get-u=patch will upgrade to the latest revision

Running go get package@version will upgrade to the specified version number version

If you download all dependencies, you can use the go mod download command.

1.6 sort out dependencies

After we delete the dependent code from the code, the related dependent libraries are not automatically removed in the go.mod file. In this case, we can use the go mod tidy command to update the dependencies in go.mod.

1.7 go mod edit formatting

Because we can modify the go.mod file manually, there are times when we need to format it. Go provides the following commands:

Go mod edit-fmt add dependency go mod edit-require=golang.org/x/text remove dependency

If you just want to modify the contents of the go.mod file, you can run go mod edit-droprequire=package path, for example, to remove the golang.org/x/text package from go.mod, use the following command:

Go mod edit-droprequire=golang.org/x/text

More uses of go mod edit can be seen at go help mod edit.

1.8 use go module1.8.1 existing projects in a project

If you need to enable go module for an existing project, you can follow these steps:

Execute go mod init under the project directory to generate a go.mod file.

Execute go get, find and record the dependencies of the current project, and generate a go.sum to record the version and hash value of each dependent library.

1.8.2 New Project

For a newly created project, we can follow these steps under the project folder:

Execute the go mod init project name command to create a go.mod file under the current project folder.

Manually edit require dependencies in go.mod or perform go get automatic discovery and maintenance of dependencies.

2. The package and the call file are under the same project

For example:

Moduledemo ├── go.mod ├── main.go └── mypackage └── mypackage.go / / package mp definition package name is mp

Steps:

1. Create a go.mod file under the project, and the file name can only be this.

two。 Add the following code to the go.mod file

Module moduledemo / / set moduledemo to the package root directory name, which can be changed at will, as long as it is consistent when importing go 1.14 / / indicates the version

3. Import the desired package file

Import "moduledemo/mypackage" / / here is the package file name under the import package directory

4. Use package files

Mp.MyPackage () / / use the MyPackage () function in the package 3. The package and the called file are not under the same project.

For example:

├── moduledemo │ ├── go.mod │ └── main.go └── mypackage ├── go.mod └── mypackage.go / / package mp definition package name is mp

Steps

1. Create the go.mod file under mypackage and add the following code

Module mypackagego 1.14

two。 Create the go.mod file under moduledemo and add the following code

Module moduledemogo 1.14require mypackage v0.0.0 / / this will automatically add replace mypackage = >.. / mypackage / / specify the required package directory in the file after you execute go build to find it in the later path.

3. Import and u

Import "mypackage" / / because the package directory itself is a package file, there is no need to add the next path mp.MyPackage () / / use the MyPackage () function in the package. Thank you for reading! This is the end of the article on "how to use go module". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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

Development

Wechat

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

12
Report