【英文】Go语言项目通过GoMod管理依赖

Introduction

Go language projects manage dependencies through GoMod.

Preparation

Enable GoMod for managing project dependencies

General

1
go env -w GO111MODULE=on

Windows

1
SET GO111MODULE=on

MacOS

1
export GO111MODULE=on

Initialize the project

  • In the project’s root directory, initialize the project with go mod init to manage the project’s dependencies with go mod.

<project_name>: Specify the project name. The project name is usually composed of domain_name/project_name/module_name, which is unrelated to the actual path name. If no project name is specified, the project name will be automatically generated based on the current directory structure.

1
go mod init <project_name>
  • At this point, two files go.mod and go.sum will be generated in the project’s root directory. The go.mod file is used to define the dependencies referenced in the project, and the go.sum file is used to verify the integrity and integrity of the dependencies.

A project that includes these two files is managed by go mod for project dependencies.
A project that does not include these two files is not managed by go mod for project dependencies.

When using Git to manage the project, it is recommended to include both the go.mod and go.sum files in Git management.

Map the project domain to local (optional)

  • If there are multiple modules in the project, you need to map the project domain of other modules to the local development environment.

Since the reference between Go language modules is done through domain names (cloud method), and the local project has not been pushed to the cloud, it is necessary to manually specify the current project as a local path during local development.

<src>: The absolute local path where the current project root directory is located.

go.mod
1
2
require <project_name> v0.0.0
replace <project_name> => <src>

Import dependencies

Manually import dependencies

  • Add the full name of the dependency package referenced by the project in the go.mod configuration file.

github.com/xxx/xxx: Full name of the dependency package.
v0.0.0: Version number of the dependency package.

go.mod
1
2
3
require (
github.com/xxx/xxx v0.0.0
)

Automatically import dependencies

  • This operation will first download the dependencies and then import them into the project.

To import into the project is actually to automatically add a configuration line in the go.mod file.
This operation is similar to npm.

github.com/xxx/xxx: Full name of the dependency package.
v0.0.0: Version number of the dependency package. If not specified, the latest version will be automatically imported.

1
go get github.com/xxx/xxx v0.0.0

Only download dependencies

  • If the project has already defined the dependencies, it only needs to download the dependencies.
1
go mod download 

Automatically update dependencies

  • This operation checks all the dependencies already imported in the project, and checks whether they have been downloaded locally. If they have not been downloaded, they will be re-downloaded locally.

Usually used for re-downloading dependencies for old projects that have not downloaded dependencies.

1
go mod tidy

Pitfalls

  • After executing the go mod tidy command in IDEA, it is possible that the code that references the dependency still reports an error. You need to double-click the directory list External Libraries/Go Modules and check all the packages obtained by downloading the dependencies. Then the code will not report an error.

Compile dependencies

  • For some Go projects, executable files are usually used instead of importing dependencies into the project. In order to use the executable files of these projects, you only need to compile the dependencies and obtain the executable files.
1
go mod install

Verify dependencies

  • After manually modifying the dependencies, verify the dependencies that have been imported.
1
go mod verify
1
go mod graph

Manually edit dependencies

1
go mod edit

Move dependencies to the vendor directory

1
vendor

Conclusion

References

Bilibili - Happy Learning with Tymon
Bilibili - A Curved Worm
Bilibili - Mole’s Documentation