Go 1.11 was officially released the day before, and this version contains two of the most important feature module web assembly . Although there are some simple tutorials about go module the features, but basically all hello world of the examples, in the course of practice, a lot of people are "desperately struggling", including myself, from some QQ group, GitHub's issue, Twitter can see everyone dazed or complaining of the statement.
Although there are three Help files go help mod , go help modules go help module-get You can understand some of the use of Go module, but feel that the Go Development Group on module This feature is not very good to do a comprehensive introduction, in many cases depends on people to see the source code or to guess, such as module download folder, version of the full declaration of the format, module the best practice, and the current implementation of Go 1.11 There are still some bugs, to everyone in the process of use has brought great difficulties.
I also in groping in the progress, recorded some of the summary of the process, hoping to give still struggling gopher some help.
Introduction to go Modules is a good introduction to the Go module, and if you read it carefully, you should not need to read this article.
Go111module
To use go module , the first to set, there is GO111MODULE=on nothing to say, if not set, the execution of the command when there will be a hint, this should all understand.
Existing projects
Suppose you already have a go project, for example $GOPATH/github.com/smallnest/rpcx , under which you can go mod init github.com/smallnest/rpcx create an empty go.mod (only the first line) under this folder module github.com/smallnest/rpcx .
Then you can find the dependencies by go get ./... making it, and record them in the go.mod file (you can also specify -tags , so that you can check the dependencies of tags found).
go mod tidyIt can also be used to go.mod remove unwanted dependencies for increased loss of dependency, but I'm not sure what to do with it tags .
Executing the above command go.mod replaces the version with the latest actual latest version, and generates a go.sum version and hash value that records each dependent library.
The new project
You can GOPATH create new projects outside of the project.
go mod init packagenameYou can create an empty one go.mod , and then you can add require github.com/smallnest/rpcx latest dependencies to it, or let go auto-discover and maintain as above.
go mod downloadYou can download the dependencies you need, but the dependencies are not downloaded to $GOPATH , but $GOPATH/pkg/mod in, multiple items can share the cached module.
Go MoD command
12345678 |
Download download modules to local cache (download dependent module to native cache)) edit edit go.mod from Tools or scripts (edit Go.mod file ) Graph Print module requirement graph (Print module dependency graph) init in current directory (again, initializes a new module under the present folder, Create Go.mod file)) Tidy add missing and remove unused modules (add missing module, remove unused module) vendor make vendored copy of Dependencies (copy dependency to Vendor) Verify Verify dependencies have expected content (check dependency) Why explain why packages or Modules is needed (explain why you need to rely on it) |
Some commands also have bugs, such as go mod download -dir :
1234 |
' Go help mod download ' for details. |
Help Rimingming say it can be set dir , but the parameters are not supported in practice dir .
The help of these commands is already easier to understand about the command's functionality.
Turn over the wall
Each package that is visited in the country needs to be golang.org/x turned over, and you can go.mod replace it with the replace corresponding library on GitHub.
12345 |
replace (Golang.org/x/ crypto V0. 0.0 -20180820150726 -614 D502A4DAC = Github.com/golang/ crypto V0. 0.0 -20180820150726 -614 D502a4dacgolang.org/x/ net V0. 0.0 -20180821023952 -922 f4815f713 = Github.com/golang/ net V0. 0.0 -20180826012351 -8 A410e7b638dgolang.org/x/ text v0. 3.0 = Github.com/golang/ text v0. 3.0 ) |
Depending on the library, it replace does not work for your Lord, for example, it go.mod github.com/smallnest/rpcx go.mod has been added replace , but your go.mod require rpcx library, but not set replace , go get will still be accessible golang.org/x .
So if you want to compile that project, add it in which project replace .
Version format
The following versions are all legal:
12345 |
Gopkg. in/tomb.v1 v1. 0. 0-20141024135613-dd632973f1e7gopkg. in/vmihailenco/msgpack.v2 v2. 9. 1gopkg. in/yaml.v2 <=v2. 2. 1github.com/tatsushid/go-fastping v0. 0. 0 -20160109021039-d7bb493dee3elatest |
Go get upgrade
- The run
go get -u will be upgraded to the latest minor version or revision (X.Y.Z, z is the revision number, Y is the minor version number)
- The run
go get -u=patch will be upgraded to the latest revision
- The run
go get package@version will be upgraded to the specified version numberversion
Go mod Vendor
go mod vendorCopy the modules download to the vendor, which seems to only download the library referenced in your code, not all of the module defined in Go.mod.
Go module, vendor and Travis CI
https://arslan.io/2018/08/26/using-go-modules-with-vendor-support-on-travis-ci/