Go1.1.1
The version has been released (released on 2018-08-24) a few days ago. From the official blog, I saw that there are two more prominent features, one is what I talked about today.module
, module concept. The function is still in the experimental stage and some places still need to be continuously improved. This support is intended to be revised continuously before the official announcement. You can remove the rightGOPATH
andgo get
Command support.
If you want to try this new feature nowmodule
, you need to put your code repository intoGOPATH/src
Outside the directory. Then create a directoryFile, run from the file tree
go
Order.
Introduction to the main concepts
module
It's a relatedGo
A collection of packages, which are the unit of source code replacement and version control. The module is formed from the source fileThe root directory definition of the file, including
The directory of a file is also called the module root.
moudles
Replace the old basedGOPATH
Method to specify which source files or import packages are used in the project. The module path is the path prefix for importing the package.The file defines the module path and lists the specific versions used during the project build process.
document
File definition
module
Paths and list other needs inbuild
when introduced a specific version of the module. For example, in the following example,statement
/m
When the pathmodule
The root directory ofmodule
Depend on a specific version/x/text
and/yaml.v2
。
module /m require ( /x/text v0.3.0 /yaml.v2 v2.1.0 )
The file can also specify the version to replace and exclude, and the command line will automatically follow
File to maintain the version in the requirement statement. If you want to get more information
Introducing the file, you can use commands
go help
。For files
//
Comments, not/**/
. Each line of the file has an instruction, consisting of an action plus a parameter. For example:
module my/thing require other/thing v1.0.2 require new/thing v2.3.4 exclude old/thing v1.2.3 replace bad/thing v1.4.5 => good/thing v1.4.5
The above three verbsrequire
、exclude
、replace
It indicates: the dependency packages and versions required by the project, excluding special versions of certain packages, and replacing some dependency packages in the current project.
Commands with the same action can be placed in a structure composed of verb + parentheses, for example:
require ( new/thing v2.3.4 old/thing v1.2.3 )
Support for other commands
Old version, build compile commandgo build
There are no parameters in-mod
Parameters, the latest version now has this one, used to matchFile update or other usage control. Form:
go build -mod [mode]
, where mode has the following values:readonly
,release
,vendor
. When executedgo build -mod=vendor
When generating the executable file, the project's dependency package will be placed in the main modulevendo
under the r directory.go get -m [packages]
The downloaded dependency package will be placed inGOPATH/pkg/mod
In the directory, and write dependencies todocument.
go get -u=patch
All dependencies under the main module will be updated.
If you encounter an unfamiliar import package, you can look for any module that contains the import package.go
The latest version of the module will be automatically added to thein the file. At the same time, missing modules will be added and useless ones will be deleted.
module
. For example:go build
, go test
orgo list
Order. In addition, there is a special commandgo mod tidy
, used to view and add missingmodule
Requirement statement and removal of unnecessary.The file is readable and editable.
go
The command line will be updated automaticallyFile to maintain a standard format and precise introduction statement.
Go mod command
Go mod
Provides a series of operationsmodules
The command, remember, allgo
The command is now built inmodule
support, not justgo mod
Order. For examplego get
When , the dependency package version will be automatically added, removed, upgraded, and downgraded in the background.
Command syntax:go mod <command> [arguments]
。Go mod
The following commands are provided, which provide detailed descriptions of more commonly used commands.
download //Download the module to the local cache. You can view it through the command go env, where the environment variable GOCACHE is the cache address. If the content of the folder is too large, you can use the command go clean -cacheedit //Edit files from tools or scriptsgraph //Print module requirements mapinit //Initialize new module in the current directorytidy //Add missing modules and remove useless modulesverify //Verify whether the dependencies have achieved the expected purposewhy //Explain why packages or modules are needed
go mod download
usage:go mod download [-dir] [-json] [modules]
. Use this command to download the specified module. The format of the module can be based on the main module dependency form orpath@version
Form specified. If no parameters are specified, this command will download all dependencies in the main module.go mod download
The command is very useful, mainly used to pre-fille local cache or calculateGo
Module Agent's answer. By default, download errors will be output to standard output, and under normal circumstances there is no output.-json
The parameters willJSON
Print the downloaded module object in the format, corresponding toGo
This is the object structure.
type Module struct { Path string //module path Version string //module version Error string //error loading module Info string //absolute path to cached .info file GoMod string //absolute path to cached .mod file Zip string //absolute path to cached .zip file Dir string //absolute path to cached source root directory Sum string //checksum for path, version (as in ) GoModSum string //checksum for (as in ) }
go mod init
usage:go mod init [module]
. This command initializes and creates a new one in the current directory.You can also create a file manually
file, then include some
module
Declare, this is more troublesome.go mod init
Commands can help us create automatically, for example:
go mod init /m
When using this command,The file must not exist in advance. Initialization will infer the path of the module based on the introduction of the package declaration or if there are already some dependency on the package management tool in your project, for example
godep
,glide
ordep
. Sogo mod init
It will also be inferred based on the dependency package management configuration file.
go mod tidy
By default, Go will not be removedUseless dependencies in the file. So when some of your dependencies are not available, you can use it
go mod tidy
command to clear it.
usage:go mod tidy [-v]
It adds missing modules and removes unwanted modules. It will be generated after executionFile (module download entry). Add parameters
-v
,For examplego mod tidy -v
The executed information, i.e. the removed module, can be printed to standard output.
go mod vendor
usage:go mod vendor [-v]
, this command willbuild
All dependencies required in the stage are placed in the main modulevendor
In the directory and test the packages for all main modules. Similarlygo mod vendor -v
Will be added tovendor
The modules in it are printed to standard output.
go mod verify
usage:go mod verify
. This command checks whether the current module's dependency has been stored in the local downloaded source code cache, and checks whether there have been any modifications since the download. If all modules are not modified, then it will be printedall modules verified
, otherwise the changed content will be printed.
Virtual version number
File and go commands usually use semantic versions as standard form to describe module versions, so that the order of different versions can be compared. For example, the version of the module is v1.2.3, then the virtual version of this version is obtained by re-labeling the version number. Form:v0.0.0-yyyymmddhhmmss-abcdefabcdef
. Where time is the UTC time at the time of commit, and the last suffix is the hash prefix of the commit. The time section ensures that the two virtual version numbers can be compared to determine the order of the two.
There are three forms of virtual version numbers below:
-
vX.0.0-yyyymmddhhmmss-abcdefabcdef
, this situation is suitable for use before the target version is submitted, without earlier versions. (This form was originally the only form, so some old files use this form) -
-pre.-abcdefabcdef
, this situation is used when the latest version commit before the target version is-pre
-
.(Z+1)--abcdefabcdef
, Similarly, this situation is when the target version is submitted before the latest version is。
You do not need to manually generate a virtual version. The go command will automatically convert the received commit hash value into the virtual version number.
Environment variables—GO111MODULE
Go 1.11
ofmodule
Support temporary environment variables—GO111MODULE
, it can set the following three values:off
,on
orauto
(default).
- if
GO111MODULE=off
,Sogo
The new command line will not be usedmodule
Function, instead, it will bevendor
In the directory andGOPATH
Look for dependency packages in the directory. This model is also calledGOPATH mode
。 - if
GO111MODULE=on
,Sogo
The command line will use itmodules
Functions without accessGOPATH
. This model is also calledmodule-aware
mode, in this mode,GOPATH
No longer herebuild
, but despite this, it still assumes the role of storing and downloading dependencies. It will put the dependency package inGOPATH/pkg/mod
In the directory. - if
GO111MODULE=auto
, this mode is the default mode, which means that if you don't set it, it isauto
. In this case,go
The command line will decide whether to enable it based on the current directory.module
Function. Only when the current directory isGOPATH/src
Outside the directory and the current directory containsThe file or its subdirectory contains
The file will be enabled.
Specific steps for use:
- First, update your version to the latest Go version (>=1.11). You can use Baidu to update the version on your own.
- Through the go command line, enter your current project directory and set temporary environment variables on the command line
set GO111MODULE=on
; - Execute the command
go mod init
Generate aFile, when executing this command, the current directory cannot exist
document.
- If it has been generated before, you need to delete it first; if there are some packages in your project that cannot be determined, then the generated
The file may be incomplete, so continue to execute the following command;
- implement
go mod tidy
command, it adds missing modules and removes unwanted modules - It will be generated after execution
File (module download entry). Add parameters
-v
,For examplego mod tidy -v
The executed information, i.e. deleted and added packages, can be printed to the command line; - Execute the command
go mod verify
Check whether all the dependencies of the current module have been downloaded and whether they have been modified after downloading. If all modules have not been modified, then after executing this command, it will printall modules verified
。 - Execute the command
go mod vendor
Generate vendor folder, which will be placed in this folderThe file description dependency package, there is also a file in the folder
, it is all modules of your entire project. Before executing this command, if you have a vendor directory before the project, you should delete it first. Similarly
go mod vendor -v
The module added to the vendor will be printed out;
Summarize
This is the article about the introduction and use of Go modules. For more information about using Go modules, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!