Author: Chen Jinjian
personal blog: HTTPS: //jian1098.github.io
CSDN blog: https: //blog.csdn.net/c_jian
Jane book: https: //www.jianshu.com/u/8ba9ac5706b6
Contact: jian1098 @qq.com
A module is a collection of related Go packages. Modules are the units 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 replace the old GOPATH-based method to specify which source files to use in a given build.
Note: Open go module
requires go1.11
and above version
Open the module
set GO111MODULE=on //windows
export GO111MODULE=on //linux
initialization
Execute the following command to generate the go.mod
file
go mod init 项目名
Execute the following command to create a vendor
directory to store and download dependencies
go mod vendor
After the execution is completed, a go.sum
file will be generated to record the lock of the version of the dependent project
Then it import
can be normal in the file that needs to use the package
Introduce a new package
In the file that needs to use the package import
, and then execute the following command again
go mod vendor
Dependent package finishing
Execute the following command to clear unused dependency packages
go mod tidy
Other commands
go mod has the following commands:
command | Description |
---|---|
download | download modules to local cache (download dependency package) |
edit | edit go.mod from tools or scripts(编辑go.mod |
graph | print module requirement graph |
init | initialize new module in current directory (initialize mod in the current directory) |
tidy | add missing and remove unused modules (pull missing modules, remove unused modules) |
vendor | make vendored copy of dependencies (copy dependencies to vendor) |
verify | verify dependencies have expected content (verify dependencies are correct) |
why | explain why packages or modules are needed |