Figure out the go moallowance/GOPATH together

Do not be stingy with your criticism and feeling, please leave a comment, we will progress together.

If you have any of the following questions, please read this article and make your comments and suggestions

  1. How to use the Go mod?
  2. What is a GOPATH?
  3. GO111MODULE=”” What does this parameter determine?
  4. What’s the difference between go get and go Download?
  5. Import what exactly is imported?

Dependency management tools

Those of you who have used Java know that managing dependencies has gone from manually importing jar packages to using automated management tools such as Maven to importing third-party dependencies so that you can use the excellent tools already developed by others. Those of you who have worked with Python may be familiar with PIP Install third-party toolkits. Both Java and Python third-party toolkits are centrally managed, with Maven or PIP downloading update dependencies from the corresponding administrator. Of course, there are dependent version tools for NPM, YARN, Gradle and other languages.

The management tools that third parties rely on in the GO language have come a long way. Before the release of GO1.11, tools like Govendor, DEP and so on blossomed. Until go Mod came along and took over the world. Go’s dependencies are very simple and crude, as long as you rely on the source code. Such as:

import  "github.com/jinzhu/gorm"

Copy the code

github.com/jinzhu/gorm is gorM’s GitHub project path.

GOPATH period

Go used the GOPATH pattern for dependency management prior to 1.11. $GOPATH=~/go = $GOPATH=~/go = $GOPATH=~/go Create a SRC /bin/pkg folder under the GOPATH path.

➜ ~ / go

├ ─ ─ binPATH=$PATH:$GOPATH/bin

├ ─ ─ PKGStore precompiled object files to speed up subsequent compilations

└ ─ ─ the SRC# store source code, Go with $GOPATH/src/github.com/foo/bar commonly the path of the deposit

Copy the code
➜ go go env | grep GOPATH

GOPATH="/Users/bytedance/go"

Copy the code

In this mode, pulling external dependencies using Go Get will automatically download and install them in the $GOPATH/ SRC directory.

In this mode, Go Get has no concept of versioning and cannot handle the problem of relying on different versions because the same dependency exists under the same path.

Before the official launch of Go Modules, Go’s dependency management tools were blooming, such as Govendor and DEP. However, the final release of Go Modules calmed down the situation of separate kingdoms.

GO Modules

Go Modules was introduced in Go1.11, and GOPATH was no longer recommended in Go1.13. This means that you can store your Go source files in any path you want, instead of having to put them in $GOPATH/ SRC. Every GO project is a Modules. Vgo is the predecessor of Go Modules.

An important environment variable, GO111MODULE, appears in the Go Modules environment

➜ ~ go env

GO111MODULE="auto"

GOPROXY="https://proxy.golang.org,direct"

GONOPROXY=""

GOSUMDB="sum.golang.org"

GONOSUMDB=""

GOPRIVATE=""

Copy the code

If you want to set environment variables for GO, you can use

go env -w GO111MODULE=on  Set the go environment variable

go env -u Restore the initial Settings

Copy the code

GO111MODULE

The GO111MODULE environment variable is used as a switch to use the Go Modules. This variable is a historical artifact and will probably be removed in future versions of Go.

GO111MODULE="auto" # Enable go Modules whenever the project includes the go.mod file, which is the default in go1.11-1.14

GO111MODULE="on"   # Enable Go Modules

GO111MODULE="off"  # Disable Go Modules for compatibility with older projects

Copy the code

GOPROXY

GOPROXY is a proxy for Go Modules that can be quickly pulled from a mirror site (centralized concept?). , you can set up multiple proxies.

GOPROXY="https://proxy.golang.org,direct"

Copy the code

Direct means that if the go get cannot be obtained through the proxy, the go get will be directly fetched through the source address

go mod

The basic command to create Go Modules

➜  ~ go mod

Go mod provides access to operations on modules.



Note that support for modules is built into all the go commands,

not just 'go mod'. For example, day-to-day adding, removing, upgrading,

and downgrading of dependencies should be done using 'go get'.

See 'go help modules' for an overview of module functionality.

All go Commands support modules.

Usage:



        go mod <command> [arguments]



The commands are:



        download    download modules to local cache

        edit        edit go.mod from tools or scripts

        graph       print module requirement graph

        init        initialize new module in current directory 

        tidy        add missing and remove unused modules

        vendor      make vendored copy of dependencies

        verify      verify dependencies have expected content

        why         explain why packages or modules are needed



Use "go help mod <command>" for more information about a command.

Copy the code

go mod init

Initialize a new module in the current directory, that is, initialize the project files in that directory as a Go Module.

If the current directory is in GOPATH, this command does not require an argument, which defaults to the relative path from GOPATH/ SRC to the directory.

➜  demo pwd

/Users/bytedance/go/src/github.com/airren/demo

➜  demo go mod init

go: creating new go.mod: module github.com/airren/demo

➜  demo ls

go.mod

➜ demo cat go. Mod

module github.com/airren/demo



Go to 1.14

➜  demo

Copy the code

If the current directory is not in GOPATH, manually specify the module name

➜  gotest pwd                                 

/Users/bytedance/Desktop/gotest

➜  gotest go mod init       If not in the GOPATH path

go: cannot determine module path for source directory /Users/bytedance/Desktop/gotest (outside GOPATH, module path must be specified)



Example usage:

        'go mod init example.com/m' to initialize a v0 or v1 module

        'go mod init example.com/m/v2' to initialize a v2 module



Run 'go help mod init' for more information.

➜  gotest go mod init github.com/airren/gotest 

go: creating new go.mod: module github.com/airren/gotest

➜ gotest cat go. Mod

module github.com/airren/gotest



Go to 1.14

Copy the code

go mod download

The go mod download command will download the package to GOPATH/ PKG /mod and specify the version to download with @latest. You can use go Mod Download in any directory

➜ jinzhu PWD/Users/bytedance/go/pkg/mod/github.com/jinzhu ➜ jinzhu ls [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] jinzhu go mod Download -x github.com/jinzhu/[email protected] # To specify the version ➜ Jinzhu ls [email protected] [email protected] [email protected] [email protected] [email protected] college [email protected] jinzhu CD [email protected]Copy the code

import

Import imports the relative path of the file store (not the absolute path) or Modules Name, not the Name of the package, but the Name of the package is used when calling Function.

Import is first looked for in $GOROOT and then in $GOPATH/ SRC if it is./ or.. / import goes directly to the corresponding relative path.

Import is case-insensitive, so in the Go project, the folder name should be lowercase as much as possible. It can have a sliding line _, but packagename must not have _, otherwise Golint will prompt you.

An example of the 🌰 file structure is shown in the figure below

gotest

├ ─ ─ format_print

│ └ ─ ─ colorprintpath. Go# filenames do not affect import

└ ─ ─ sdemo

├ ─ ─ demo. Go

Copy the code

colorprintpath.go

package colorprintfile // The method is called by package name

import "fmt"

// NewPirnt is the new format print

func NewPrint(content string) {

 fmt.Printf("This is the content: %v \n", content)

}

Copy the code

Demo. go calls the above method

package main

import  ".. /format_print"  // import uses relative paths

func main(a){

 colorprintfile.NewPrint("hello")  // Call the public method in the package

}

Copy the code

If you use go mod, you have go.mod in the project file, and it will be in the file

module github.com/airren/gotest

Copy the code

Instead of referring to the package using a relative path, you refer to it via modules

package main

import  "github.com/airren/gotest/format_print"

func main(a){

 colorprintfile.NewPrint("hello")  // Call the public method in the package

}

Copy the code

go get

You can execute Go get in any path

  • Use to download and install code packages from remote code repositories (Github, GitLab, GOgs)

  • The supported code version control systems are Git, Mercurial(HG), SVN, and Bazaar

  • The specified code package is downloaded to the SRC directory in the first workspace contained in $GOPATH, and then installed

    Commonly used parameters

    -d         # install install install install install

    -fix       Perform the fix action after downloading the package, then compile and install it

    -u         Use the network to update existing code packages and their dependencies

    -x         # display process

    Copy the code

For example, use Go Get to obtain gorM. The -x argument shows the detailed process.

➜ ~ go get-u-x github.com/jinzhu/gorm

cd .

git clone -- https://github.com/jinzhu/gorm /Users/bytedance/go/src/github.com/jinzhu/gorm

cd /Users/bytedance/go/src/github.com/jinzhu/gorm

git submodule update --init --recursive

cd /Users/bytedance/go/src/github.com/jinzhu/gorm

git show-ref

cd /Users/bytedance/go/src/github.com/jinzhu/gorm

git submodule update --init --recursive

cd /Users/bytedance/go/src/github.com/jinzhu/inflection

git config remote.origin.url

cd /Users/bytedance/go/src/github.com/jinzhu/inflection

git pull --ff-only

cd /Users/bytedance/go/src/github.com/jinzhu/inflection

git submodule update --init --recursive

cd /Users/bytedance/go/src/github.com/jinzhu/inflection

git show-ref

cd /Users/bytedance/go/src/github.com/jinzhu/inflection

git submodule update --init --recursive

WORK=/var/folders/pz/w7jm4wm933lcspm82kff26600000gn/T/go-build644292157

Copy the code

If have go. Mod project folder using the go get, will depend on and version of the corresponding written. Mod, go. The sum is generated automatically, can view https://studygolang.com/articles/25658 in detail

➜ gotest cat go. Mod

module github.com/airren/gotest



Go to 1.14

➜ gotest cat go. Sum

cat: go.sum: No such file or directory

➜  gotest go get -u github.com/jinzhu/gorm/  # pull gorm

Go: github.com/jinzhu/gorm upgrade => v1.9.12

➜ gotest cat go. Mod

module github.com/airren/gotest



Go to 1.14



Require github.com/jinzhu/gorm v1.9.12 // indirect# an indirect dependency is an indirect dependency

➜ gotest cat go. Sum

Github.com/denisenkom/go-mssqldb v0.0.0 FFD - 20191124224453-732737034 / go mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=

Github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8 d10e4a1bae5 / go mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=

Github.com/go-sql-driver/mysql v1.4.1 / go mod h1: zAC/RDZ24gD3HViQzih4MyKcchzm + sOG5ZlKdlhCg5w =

Github.com/golang-sql/civil v0.0.0-20190719163853 - cb61b32ac6fe/go. Mod h1:8 vg3r2vgvsthlbifl93qb5ywzgyzwhembwujwevakk0 =

Github.com/golang/protobuf v1.2.0. / go mod h1:6 lqm79b + lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U =

Github.com/jinzhu/gorm v1.9.12 h1: Drgk1clyWT9t9ERbzHza6Mj / 8 fy/CqMyVzOiHviMo6Q =

Github.com/jinzhu/gorm v1.9.12 / go mod h1: vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs =

Github.com/jinzhu/inflection v1.0.0 h1: K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E =

Github.com/jinzhu/inflection v1.0.0 / go mod h1: h + + Qp1Va5pdKtLDYj uFLlag + kHp5pxUVkryuEj + Srlc =

Github.com/jinzhu/now v1.0.1 / go mod h1: d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8 =

Github.com/lib/pq v1.1.1. / go mod h1:34 woo = 5 wuzqawbwv1u + lTReE5YruASi9Al49XbQIvNi /

Github.com/mattn/go-sqlite3 v2.0.1 + incompatible/go. Mod h1: FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc =

Golang.org/x/crypto v0.0.0-20190308221718 - c2843e01d9a2 / go. Mod h1: djNgcEr1 / C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0 + w =

Golang.org/x/crypto v0.0.0-20190325154230 - a5d413f7728c/go. Mod h1: djNgcEr1 / C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0 + w =

Golang.org/x/crypto v0.0.0-20191205180655 - e7c4368fe9dd/go. Mod h1: LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto =

Golang.org/x/net v0.0.0 e40ba225-20180724234803-3673 / go mod h1: mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4 =

Golang.org/x/net v0.0.0-20190404232315 - eb5bcb51f2a3 / go. Mod h1: t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg =

Golang.org/x/sys v0.0.0-20190215142949 - d0b11bdaac8a/go. Mod h1: STP8DvDyc/dI5b8T5hshtkjS + E42TnysNCUPdjciGhY =

Golang.org/x/sys v0.0.0-20190412213103-97732733099 - d/go. Mod h1: h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =

Golang.org/x/text v0.3.0 / go mod h1: NqM8EUOU14njkJ3fqMW + pc6Ldnwhi/IjpwHt7yyuwOQ =

Google.golang.org/appengine v1.4.0. / go mod h1: xpcJRLb0r/rnEns0DIKYYv + WjYCduHsrkT7 / EB5XEv4 =



Copy the code

Do not be stingy with your criticism and feeling, please leave a comment, we will progress together.

References:

https://github.com/golang/go/wiki/Modules

https://blog.golang.org/using-go-modules

https://juejin.cn/post/6844903433846145038

https://learnku.com/go/t/39086

https://studygolang.com/articles/25658