GO111MODULE function

One,

  • When there is no GO111MODULE, the order in which the go compiler finds dependencies is go path> goroot. With GO111MODULE, the go.mod. file of the current project will be read, and the go.mod file will record which dependencies are there

  • Go Modules is a dependency management method of the Go language. This feature appeared in the Go 1.11 version. In recent projects, the team has begun to use go modules to replace the previous Godep. Kubernetes also starts from v1.15 Go module is used for package management, so it is necessary to learn about go module.

  • Compared with the original Godep, go module has obvious speed advantages in multiple links such as packaging and compilation, and can easily reproduce dependent packages on any operating system. More importantly, the design of go module itself makes It has become easier to be cited by other projects, which is another important manifestation of the evolution of the Kubernetes project to framework.

two,

  • After using go module to manage dependencies, two files go.mod and go.sum will be generated in the project root directory.
    The dependencies of the current project will be recorded in go.mod, and 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 records the version and hash value of each dependent library. The 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=

3. How to enable go module function

(1) go version >= v1.11
(2) Set GO111MODULE environment variable
  • To use go module, you must first set GO111MODULE=on. GO111MODULE has three values, off, on, auto, off and on are turned off and on, and auto will determine whether to use the modules function based on whether there is a go.mod file in the current directory.

  • No matter which mode is used, the module function does not look for dependent files in the GOPATH directory by default, so please set up the proxy when using the modules function.

  • When using go module, set the GO111MODULE global environment variable to off, and then turn it on when you need to use it to avoid accidentally introducing go module into existing projects.

download download modules to local cache (下载依赖的module到本地cache))
edit edit go.mod from tools or scripts (编辑go.mod文件)
graph print module requirement graph (打印模块依赖图))
init initialize new module in current directory (在当前文件夹下初始化一个新的module, 创建go.mod文件))
tidy add missing and remove unused modules (增加丢失的module,去掉未使用的module)
vendor make vendored copy of dependencies (将依赖复制到vendor下)
verify verify dependencies have expected content (校验依赖)
why explain why packages or modules are needed (解释为什么需要依赖)

Fourth, use the go module function

1. Use go module for new projects:
  • $ export GO111MODULE=on
    $ go mod init github.com/you/hello
  • // go build will add the dependencies of the project to go.mod
    $ go build
2. For existing projects, change to use go module:
  • $ export GO111MODULE=on
    // Create an empty go.mod file
    $ go mod init.

  • // Find dependencies and record them in the go.mod file
    $ go get ./... The
    go.mod file must be submitted to the git repository, but the go.sum file does not need to be submitted to the git repository (gi t ignore the file. Gitignore to set it up ).

Five, the packaging of the project

  • First, you need to use go mod vendor to download all the dependencies of the project to the local vendor directory and then compile it. Here is a reference:
go mod vendor
go build -ldflags “-s -w” -a -installsuffix cgo -o audit-webhook .

Six, matters needing attention

1. Depend on download
  • By default, go module does not search for dependent files in the GOPATH directory. It will first search for required dependencies in GOPATH / pkg / mod, and download directly if there is none.
  • You can use gomoddownload to download the required dependencies. By default, the dependencies will be downloaded to GOPATH/pkg/mod to find out if there are any required dependencies. If not, they will be downloaded directly.
  • You can use go mod download to download the required dependencies. By default, the dependencies will be downloaded to GOPATH/pkg/mod to find out if there are any required dependencies. If not, they will be downloaded directly.
  • You can use gomoddownload to download the required dependencies. The dependencies will be downloaded to GOPATH/pkg/mod by default, and other projects will also use cached modules.

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/115154147