Go moudules is a package management tool of go. It is officially provided and reliable. The minimum go version requires 1.11 +.
It can be understood as Python virtual environment, Java Maven and PHP composer.
Its essence is to solve the problem of package management confusion.
Anyway, it’s done with it. There are only good things, not bad things.
Configuration (no pipe)
- Go111module = off # not applicable to modules function
- Go111module = off # using the molules function, you will not find dependent packages under gopath. Version 1.11 + is enabled by default
- Go111module = Auto: does golang #golang use the modules function
According to the above commands, it can be concluded that in version 1.11 +, go modules can be used by default without other settings.
What is the difference between it and the traditional direct go get
When go modules is not used, the packages we download directly from go get will be placed in the% gopath% / SRC directory by default.
When go modules is enabled, packages will be downloaded to the% gopath% / PKG / mod folder.
And it will carry the version number, which basically completely solves the dependency problem.
Create a go.mod file
The go.mod file is equivalent to the requirement.txt file, which will put some required dependent packages.
I created a gomod demo folder.
And CMD enters this folder.
Execute the command go mod init gomod demo.
The later gomod demo can be written freely. It is recommended to be the same as the folder name.
After success, a go.mod file will be added to the folder to represent success.
I use GoLand to open this gomod demo folder.
Go.mod file description
- Module: module name.
- Require: dependent package list and version.
- Exclude: prohibit dependency package list (effective only when the current module is the main module).
- Replace: replace the dependent package list (effective only when the current module is the main module).
Of course, we don’t need to move these basically. They are modified through commands or IDE.
For example, I need to use an xstrings package.
GitHub address: https://github.com/huandu/xstrings 。
You need to execute this command to install this package.
Execute the download command.
The go.mod file will automatically add a dependent package.
- s: = “near hehedsl”
Go connect to MySQL
The GitHub documentation is as follows.
Execute the download command go get – u github.com/go-sql-driver/mysql
The go.mod file is as follows.
- //s: = “near hehedsl”
- db，err:=sql.Open(“mysql”，”user:[email protected]/dbname”)
If you use the go get third-party package, GoLand cannot import it, or an error is reported, or there is no goModules。
In the project directory, you can basically solve the problem by using go mod download. As long as there are go modules, it means there is no problem.
If there is still no go modules, set – > go – > go modules (VGO) in Golan
Go mod all commands
- Initialize a new module in the current folder and create a go.mod file: gomodinitname
- Pull the missing module and remove the unused module: gomodtidy
- Copy dependency to vendor: gomodvendor
- Download dependency: gomoddownload
- Test dependency: gomodverify
- Display module dependency graph: gomodgraph
- Explain why dependency is needed: gomodwhy
- Edit the go.mod file: goeidt
- View command list: gomod
- View the command help document: gohelpmod
Above, we learned how to use go mod. If there is a task problem during the operation, remember to leave a message in the discussion area below, and we will solve the problem at the first time.