Home  >  Article  >  Backend Development  >  Detailed explanation of using Go Modules

Detailed explanation of using Go Modules

尚
forward
2020-01-04 16:59:562358browse

Detailed explanation of using Go Modules

Go Modules Introduction

Modules is a new experimental feature in Go 1.11. It is a new package management tool based on the evolution of vgo.

Package management tools before Go 1.11 were all based on PATH or vendor directories, which could not solve the problem of dependency on different versions very well. Modules is a new package management method outside of GOPATH. Using Modules makes it completely independent of GOPATH.

Note: Go Modules requires Go version 1.11 or above

Usage Guide

Example

1. Create a directory outside GOPATH

$ mkdir -p /tmp/scratchpad/hello
$ cd /tmp/scratchpad/hello

2. Initialize Module

$ go mod init github.com/you/hello
go: creating new go.mod: module github.com/you/hello

3. Edit hello.go

package main
import (
    "fmt"
    "rsc.io/quote"
)
func main() {
    fmt.Println(quote.Hello())
}

4. Compile and run

$ go build
$ ./hello
Hello, world.

Note: go build will automatically download dependency packages, while golang Packages such as .org/x/... need to circumvent the firewall. You can add the GOPROXY environment variable to set the proxy:

export GOPROXY=https://goproxy.io

At this time, the go.mod file will be automatically updated and it contains the dependencies required by the project. and the corresponding version number

$ cat go.mod
module github.com/you/hello
require rsc.io/quote v1.5.2

In addition to go.mod, go also maintains a file called go.sum, which contains the expected hash value of a specific module version:

cat go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

go uses The go.sum file ensures that future downloads of these modules are the same as the first download, thus ensuring that there are no unexpected changes to the modules your project depends on.
Both go.mod and go.sum should be checked into version control.

Daily WorkFlow

A typical daily workflow may be:

Add import statements in the .go code as needed

Standard commands (such as go build or go test) will automatically add new dependencies as needed (update go.mod and download new dependencies)

When you need a specific version, you can use go get to specify a specific version Such as go get foo@v1.2.3, go get foo@master, go get foo@e3702bed2, or directly edit the go.mod file

Commonly used commands:

go list -m all View All direct and indirect dependencies in the project

go mod tidy Clear unused dependencies and add other required dependencies

go mod vendor Copy dependencies to the vendor directory

For more go language knowledge, please pay attention to the PHP Chinese website go language tutorial column.

The above is the detailed content of Detailed explanation of using Go Modules. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete