Home >Backend Development >Golang >An article teaches you how to use Go language Modules
go moudules is a package management tool for Go. It is officially provided and is relatively reliable. , the minimum Go version required is 1.11.
can be understood as Python’s virtual environment, or Java’s maven, or PHP’s composer.
The essential problem it solves is the problem of package management confusion.
Anyway, just use it and that’s it. There are only good things and no bad things.
GO111MODULE=off # 不适用modules功能 GO111MODULE=off #使用molules功能,不会去GOPATH下找依赖包, 1.11+版本默认开启 GO111MODULE=auto:Golang # Golang自己简则是不是使用modules功能
Based on the above command, it can be concluded that in version 1.11, go modules can be used by default, and no other settings are required.
When go modules are not used, we directlygo get
The downloaded package will be placed in the %GOPATH%/src
directory by default.
After enabling go modules, the packages will be downloaded to %GOPATH%/pkg/mod
folder.
And it will have a version number, which basically completely solves the dependency problem.
go.mod
# The ## file is equivalent to the requirement.txt file, which will contain some required dependency packages.
I created agomod-demo folder.
andCMDenter this file Clip inside.
Execute the command go mod init gomod-demo
.
The following gomod-demo
can be written casually. It is recommended that it be the same as the folder name.
After success, there will be one more #go.mod
File, represents success.
##I used Goland to open thisgomod-demo folder.
module: module name.
require: Dependency package list and version.
exclude: prohibits dependent package list (only takes effect when the current module is the main module).
replace: Replace the dependent package list (only takes effect when the current module is the main module).
Of course, we don’t need to touch these basics, they are all modified through commands or IDE.
For example, I need to use a xstrings package.
github address:https://github.com/huandu/xstrings.
You need to execute this command to install this package.
执行下载命令。
go.mod文件会自动添加一个依赖包。
代码:
package main import ( "fmt" "github.com/huandu/xstrings" ) func main() { s := "heheh ADSL附近" center := xstrings.Count(s, "h") fmt.Println(center) }
github文档如下。
执行下载命令 go get -u github.com/go-sql-driver/mysql
go.mod文件如下。
代码:
package main import ( "database/sql" _ "github.com/go-sql-driver/mysql" "time" ) func main() { //s := "heheh ADSL附近" //center := xstrings.Count(s, "h") //fmt.Println(center) db, err := sql.Open("mysql", "user:password@/dbname") if err != nil { panic(err) } // See "Important settings" section. db.SetConnMaxLifetime(time.Minute * 3) db.SetMaxOpenConns(10) db.SetMaxIdleConns(10) }
如果使用go get 第三方包
时,goland无法导入,或者报错,或者没有Go Modules
。
在项目目录下,使用go mod download
下载一下,基本可以解决问题,只要有Go Modules
,就代表没问题。
如果还是没有Go Modules
,在Goland中settings->Go->GoModules(vgo)
在当前文件夹下初始化一个新的 module,创建 go.mod 文件:go mod init name 拉取缺少的模块,移除不用的模块:go mod tidy 将依赖复制到 vendor 下:go mod vendor 下载依赖:go mod download 检验依赖:go mod verify 显示模块依赖图:go mod graph 解释为什么需要依赖:go mod why 编辑 go.mod 文件:go eidt 查看命令列表:go mod 查看命令帮助文档:go help mod
The above is the detailed content of An article teaches you how to use Go language Modules. For more information, please follow other related articles on the PHP Chinese website!