[Environment] go mod--a love-hate package management tool

Keywords: Go github Python pip npm

Why do we need package management

Python has pip, Nodejs has npm.Is there a package management tool in other languages, so go needs it?Of course not, let's look at the following code:

import (
"github.com/TomatoMr/something"
)

This is how go references packages, which is obviously a third-party package, so how exactly is it found on our machine?
First of all, when there is no go mod, it looks like this:

1. If there is a vendor in the project root directory, look under vendor.
2. If vendor cannot find the package, go to GOROOT, $GOPATH; 3. If you can't find any of these, go online and look for them, such as "github.com/TomatoMr/something", then go to GitHub. Once the package is found, it will be downloaded to GOPATH, which is easy to understand.But that's not enough. For example, when I have a need for version control on these packages, there's still something missing in this package management approach. The scenario is as follows: The third package that I refer to has different versions. If I don't use other package management tools, the package I get by default is the latest version, that is, version v0.0.2. At that time, the version of the HI() method no longer exists, and I still call the Hi() method in my code, which will cause my code to error. I now need a version-controlled package management tool. Gomod is coming Since version 1.11 of go, go mod has been designated as your own package management tool, providing a more flexible way to manage packages, allowing you to version control them, and your project does not necessarily need to be under GOPATH (there's nothing wrong with GOPATH).See how it works: $ go mod init <module_name> # Initialize the module of a project, module_name is optional, you can make the module name at the time of initialization
$go mod tidy # Add packages, clear packages without references After executing two commands, you will see that there are more go.mod and go.sum files under the project. go.mod: Can be understood as package management file go.sum: Can be understood as a version control file for a package Okay, now let's see what we're looking for, what I want to do with gomod when I want to bring in packages.Let's first change the version number in the go.mod file to v0.0.1. Then execute at the project root $ go mod tidy

We can see that the go.sum file has changed accordingly.

Looking at our code again, there was no error calling the Hi() method.

Master some basic knowledge of go mod

1. You need to learn what go mod is

Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

go mod <command> [arguments]

The commands are:

edit        edit go.mod from tools or scripts # Modify or replace package
graph       print module requirement graph # Print Dependency
init        initialize new module in current directory # Initialize project
tidy        add missing and remove unused modules # Update Dependent Packages
vendor      make vendored copy of dependencies # Copy dependent packages to vendor
verify      verify dependencies have expected content # Check that dependencies are correct
why         explain why packages or modules are needed # Explain why packages or modules are needed, it should be said that those modules call those dependent packages and modules

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

2. Some environment variables should know

GO111MODULE=
# By default, go mod is used to manage go.mod files in a project, but not in the old way. GO111MODULE=on enforces go mod, GO111MODULE=off closes go Mod
GOPROXY="https://goproxy.cn,https://proxy.golang.org,https://goproxy.io,direct"
# GOPROXY set proxy, I recommend to set it to the same as above, the first address is Qiniuyun proxy, to prevent being walled, direct is worth downloading from the source site
GOSUMDB="sum.golang.org"
# Indicates the address and public key of the check and server, where the public key for "sum.golang.org" can be omitted. To turn off the check, GOSUMDB=off.
GOPRIVATE=""
# GOPRIVATE represents a private warehouse.All dependencies under the private repository are downloaded from the source site without verification.
GONOPROXY=""
# Corresponds to GOPROXY
GONOSUMDB=""
# Corresponds to GOSUMDB

There are practical benefits to familiarizing yourself with these environment variables, such as the inability to download packages from foreign websites for some unspecified reason, or the need to configure a private warehouse built by your company.

3. Replace packages with replace

There are scenarios such as:

2. You have referenced a local package that has not been submitted to the repository.

At this point, you need to replace the package as follows:

package main

import (
"github.com/TomatoMr/bar" # There is no package under this address. Maybe the original package is renamed or this is a local package.
"github.com/TomatoMr/something"
)

func main()  {
something.Hello()
something.Hi()
bar.HelloBar()
}

See how we can modify it in the go.mod file:

module Foo

go 1.13

require (
github.com/TomatoMr/something v0.0.1
)

replace github.com/TomatoMr/bar => D:\\share\\go\\src\\foo\\bar # Replace bar with my local path (absolute or relative)

Then execute:

go mod tidy

Then go.mod changes:

module Foo

go 1.13

require (
github.com/TomatoMr/bar v0.0.0-00010101000000-000000000000
github.com/TomatoMr/something v0.0.1
)

replace github.com/TomatoMr/bar => D:\\share\\go\\src\\foo\\bar

You can see that github.com/TomatoMr/bar has require d in.

go clean -modcache