[Articles] go mod-- environment people love to hate package management tool

Cover .png

Why do we need to package management

Python has pip, Nodejs have npm. There is not a language other package management tools, so we should go there? Of course not, take a look at the following code:

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

This is the way to go package quote, it is clear that this is a third-party packages, specifically how it is found on this machine we do?
Let us talk time has not yet go mod, and it is such a find:

  1. There are vendor project root directory, then look under the vendor;
  2. vendor can not find the package aboard GOROOT, $ GOPATH find;
  3. All of the above can not be found, go online to find, such as "github.com/TomatoMr/something", then look on GitHub go.

After finding the package will download it to GOPATH at, easy to understand. But this is not enough, such as when I had control of demand versions of these packages, then this package management are the lack of specific scenario is as follows:

GitHub-1.png

I quote the third package has a different version, if I do not have other package management tools, then I get the default package is the latest version, which is the version v0.0.2, and then the release of Hi () method has not exist, but my code still calls inside the Hi () method, which will make my code error, I now need to have a package management tool for version control.

error.png

go mod here

After the 1.11 version of go, go mod was designated as a natural package management tool that provides a more flexible management practices, can be versioned package, and you do not need to necessarily put the project under the GOPATH (put nothing too bad at GOPATH). It is a look at how specific do's:

$ go mod init <module_name> # 对一个项目的module进行初始化,module_name是选填的,可以在初始化的时候就制定module名
$ go mod tidy # 添加包,清除没有引用的包

gomod-1.png

After completion execute two commands, you will see more go.mod and go.sum project files.

go.mod: can be understood as a package management file
go.sum: it can be understood as version control file package

Well, we now take a look at our previous demands, I want to be able to select the version of the package introduced by gomod how to do it. We first go.mod file version number was changed: v0.0.1.

gomod-2.png

Then execute the project root directory

$ go mod tidy

We can see go.sum file accordingly changed.

gomod-3.png

Look at our code, call the Hi () method is not being given.

gomod-4.png

Have some basic knowledge of go mod

1. The need to learn what is 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.

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 # 复制依赖包到vendor
        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.

After using go mod, after downloading the package is placed under $ GOPATH / pkg / mod:

gomod-5.png

2. Some environment variables should know

GO111MODULE=
# 默认当项目中有go.mod文件就用go mod管理,没有就还是旧的方式,GO111MODULE=on强制使用go mod,GO111MODULE=off关闭go mod
GOPROXY="https://goproxy.cn,https://proxy.golang.org,https://goproxy.io,direct"
# GOPROXY设置代理,我建议设置成与我上面一致,第一个地址是七牛云的代理,防止被墙,direct值得是从源站下载
GOSUMDB="sum.golang.org"
# 指示校验和服务器的地址和公钥,这里指"sum.golang.org"的公钥可省略,若要关闭校验,GOSUMDB=off。
GOPRIVATE=""
# GOPRIVATE表示私有仓库。私有仓库下的所有依赖一律从源站下载,而且不做校验。
GONOPROXY=""
# 与GOPROXY对应
GONOSUMDB=""
# 与GOSUMDB对应

Familiar with these environment variables have practical benefits, such as for some reason can not be described but can not download the pack of foreign sites, or because of his private company to build a warehouse, need to do the appropriate configuration.

3. replace ~ package

There is such a written application scenarios, such as:

  1. You have to download the package changed his name;
  2. You quoted a local package, which was not submitted to the warehouse;

This time you need to replace to replace the package, as follows:

package main

import (
    "github.com/TomatoMr/bar" # 在这个地址下面没有这个包,可能是原包换名字或者这是我一个本地包
    "github.com/TomatoMr/something"
)

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

We should look at how changes in go.mod file inside:

module Foo

go 1.13

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

replace github.com/TomatoMr/bar => D:\\share\\go\\src\\foo\\bar # 将bar这个包替换成我的本地路径(绝对路径或者相对路径均可)

Then execute:

go mod tidy

Then go.mod changed:

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

We can see github.com/TomatoMr/bar already require into it.

4. Clear mod download package

go clean -modcache

This is rarely used, if your environment is damaged, or if you want to delete all the mod cache, you can do it.


I welcome the attention of the public number: onepunchgo , will be the compilation of relevant documents and information.

image

Guess you like

Origin blog.51cto.com/14664952/2466832