premise
go to version 1.13 and above
Official Documents
If you want a deeper understanding of the meaning and GO MODULE developers concerns, direct access to official documents (EN)
https://github.com/golang/go/wiki/Modules
go module Introduction
go module that comes with the official go go dependency management library, officially recommended in the 1.13 version
go module can be organized into a go.mod all dependent files in a project (folder), which rely on written version, etc.
After using go module we can not place the code in the src
After using the go module management rely on two files are generated go.mod and go.sum in the project root directory.
go.mod will depend for the current project, the file format is as follows:
module github.com/gosoon/audit-webhook
go 1.12
require (
github.com/elastic/go-elasticsearch v0.0.0
github.com/gorilla/mux v1.7.2
github.com/gosoon/glog v0.0.0-20180521124921-a5fbfb162a81
)
go.sum record version and the hash value of each dependent library file format is as follows:
github.com/elastic/go-elasticsearch v0.0.0 h1:Pd5fqOuBxKxv83b0+xOAJDAkziWYwFinWnBO0y+TZaA=
github.com/elastic/go-elasticsearch v0.0.0/go.mod h1:TkBSJBuTyFdBnrNqoPc54FN0vKf5c04IdM4zuStJ7xg=
github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I=
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gosoon/glog v0.0.0-20180521124921-a5fbfb162a81 h1:JP0LU0ajeawW2xySrbhDqtSUfVWohZ505Q4LXo+hCmg=
github.com/gosoon/glog v0.0.0-20180521124921-a5fbfb162a81/go.mod h1:1e0N9vBl2wPF6qYa+JCRNIZnhxSkXkOJfD2iFw3eOfg=
Open go module
(1) go version> = V1.11
(2) set the environment variable GO111MODULE
To go module first set GO111MODULE = on, GO111MODULE three values, off, on, auto, off and on and turn off i.e., auto document according go.mod whether there will be in the current directory to determine whether to use the function modules. No matter which mode to use, module features default look for a dependency file is not GOPATH directory, so please use the modules function set a good agent.
When using the go module, the GO111MODULE global environment variable is set to off, turn it on when you want to use, avoid accidental introduction go module in an existing project.
windows:
set GO111MODULE=on
mac:
export GO111MODULE=on
Then enter
go env
View GO111MODULE option to modify success on behalf of
GO PROXY
The purpose go module is dependent on management, so you can go module when using discarded go get command (but not prohibit the use, if you want to specify the version of the package or the update package may use go get, usually no need to use)
Due to network problems go, it is recommended to set goproxy.cn
// 阿里云镜像
GOPROXY=https://mirrors.aliyun.com/goproxy/
// 中国golang镜像
GOPROXY=https://goproxy.io
initialization
For your first project using the GO MODULE (projects not yet go.mod file)
Into your project folder
cd xxx/xxx/test/
Initialization MODULE
go mod init test(test为项目名)
We will find that there will be a go.mod file in the root directory of the project
Note that at this time go.mod document only identifies the project name and go version, this is normal, because it is only initialized
Detection relies
go mod tidy
tidy detects the folder depend all incoming directory, file written go.mod
After writing you will find go.mod document subject to change
E.g:
module test
go 1.13
require (
github.com/gin-contrib/sessions v0.0.1
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.4.0
github.com/go-redis/redis v6.15.6+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/golang/protobuf v1.3.2 // indirect
github.com/jinzhu/gorm v1.9.11
github.com/json-iterator/go v1.1.7 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/sirupsen/logrus v1.2.0
github.com/ugorji/go v1.1.7 // indirect
golang.org/x/sys v0.0.0-20191025021431-6c3a3bfe00ae // indirect
gopkg.in/yaml.v2 v2.2.4
)
In this case dependent or not to download
Download dependent
We need to rely downloaded to a local, rather than using go get
go mod download
If you do not set GOPROXY for domestic image, this step will be hundred percent to the death card
At this time will depend on all downloaded to GOPATH next will be generated in the root directory go.sum file, which is a detailed dependent dependent, but we said at the beginning, our project is not put under GOPATH, then we Download GOPATH is next to useless, still can not find these packages
Import dependence
go mod vendor
This command is executed, will depend on just downloaded to transfer the project to the vendor under the root directory of GOPATH (automatically created) folder, then we can use these depend on the
goland set to open Go Module
Probably because GO MODULE need to improve function, GOLAND this feature is off by default, we need to manually open (update will not change enabled by default after not ruled out)
Go- to open the settings> unfold -> select Go Modules (vgo) -> select both boxes filled mirror source selection box in the Proxy
Reliance update
Update here does not refer to the version of the update, but to rely on the introduction of new
Please update has been executed can rely on from the detection relies parts, namely,
go mod tidy
go mod download
go mod vendor
New dependence
Some students will ask, do not use go get, how do I add a new package in the project do?
Direct project import this package, you can rely on after the update
Use GO MODULE in collaboration
Note that, in project management, such as the use git, please vendor folder in the white list, or else the project will bring great package volume
git whitelist way to git hosted in the root directory of the new project file .gitignore
Settings can be ignored.
But do not ignore go.mod and go.sum
Dependency update (update with top-dependent) in a local project to clone after another person
GOMODULE commonly used commands
go mod init # 初始化go.mod
go mod tidy # 更新依赖文件
go mod download # 下载依赖文件
go mod vendor # 将依赖转移至本地的vendor文件
go mod edit # 手动修改依赖文件
go mod graph # 打印依赖图
go mod verify # 校验依赖