Foreword
go version 1.13 as well, has been very criticized dependency management has also been the official direction, but saw a lot of blog posts about the current or older.
So do here to go mod a rough note
text
premise
go to version 1.13 and above
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
Specific please read
Open go module
When default auto go in version 1.13, when the project on behalf of outside GOPATH / src and the project root directory go.mod file, open the go module.
That is, if you do not place the code in GOPATH / src MODULE using the default management.
We can also manually change on (all open) / off (not all open)
Here demo set on
windows:
set GO111MODULE=on
mac:
export GO111MODULE=on
Then enter
go env
View GO111MODULE Options
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 detailed goproxy.cn
https://github.com/goproxy/goproxy.cn/blob/master/README.zh-CN.md
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 the project name)
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 is hundred percent will live rammed to death
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, it will download just to rely in GOPATH transferred to the vendor under the project root directory (automatically created) folder
At this point 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)
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 GOMODULE 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
MOD the init go # initialize go.mod go MOD Tidy # update the dependent files go download MOD # download dependent files go MOD vendor # will depend transferred to a local vendor file go Edit MOD # to manually modify dependent files go MOD Graph # print dependency graph go verify MOD # checksum dependent