Go language projects manage dependencies through GoMod.
Enable GoMod for managing project dependencies
go env -w 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.
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.
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.
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.
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.
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.
go mod tidy
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.
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.
go mod install
After manually modifying the dependencies, verify the dependencies that have been imported.