What is go module? how to use? (with usage examples)

藏色散人
Release: 2022-01-12 16:02:39
forward
5414 people have browsed it

This article is provided by the golang tutorial column to introduce you to the use of go module. I hope it will be helpful to friends in need!

go module usage

go module was launched after go 1.14 version

1. Introduction to go module usage

go module is the version management tool officially launched after Go1.11 version, and starting from Go1.13 version, go module will be the default dependency management tool of Go language.

1.1 GO111MODULE

To enable go module support, you must first set the environment variable GO111MODULE, through which you can turn on or off module support. It has three Optional values: off, on, auto, the default value is auto.

  • GO111MODULE=offDisables module support and will look for packages from the GOPATH and vendor folders when compiling.

  • GO111MODULE=onEnable module support, the GOPATH and vendor folders will be ignored when compiling, only based on go.modDownload dependencies to the %GOPATH%/pkg/mod/ directory.

  • GO111MODULE=auto, when the project is outside $GOPATH/src and the project root directory has go.mod file, enable module support.

Simply put, after setting GO111MODULE=on, you can use go module. There is no need to create projects in GOPATH in the future. , and can also well manage the third-party package information that the project depends on.

After using go module to manage dependencies, two files go.mod and go.sum will be generated in the project root directory.

1.2 Republican /proxy.golang.org

is inaccessible in China, so it is highly recommended that you set up GOPROXY. Here I recommend using goproxy.cn.

export GOPROXY=https://goproxy.cn
Copy after login
1.3 go mod command

Commonly used go modcommands are as follows:

go env -w GOPROXY=https://goproxy.cn,direct
Copy after login
1.4 go.mod

go.mod The file records all the dependency information of the project, and its structure is roughly as follows:

go mod download    下载依赖的module到本地cache(默认为$GOPATH/pkg/mod目录)
go mod edit        编辑go.mod文件
go mod graph       打印模块依赖图
go mod init        初始化当前文件夹, 创建go.mod文件
go mod tidy        增加缺少的module,删除无用的module
go mod vendor      将依赖复制到vendor下
go mod verify      校验依赖
go mod why         解释为什么需要依赖
Copy after login
Among them,

module

is used to define the package name

require
    Used to define dependent packages and versions
  • indirect
  • Indicates indirect reference
  • 1.4.1 Dependent version
  • go mod supports semantic version numbers, such as go get foo@v1.2.3, or it can be followed by git branches or tags, such as
  • go get foo@master
, of course it can also be followed git commit hash, such as

go get foo@e3702bed2

. Regarding dependent versions, the following formats are supported:

module github.com/Q1mi/studygo/blogger

go 1.12

require (
    github.com/DeanThompson/ginpprof v0.0.0-20190408063150-3be636683586
    github.com/gin-gonic/gin v1.4.0
    github.com/go-sql-driver/mysql v1.4.1
    github.com/jmoiron/sqlx v1.2.0
    github.com/satori/go.uuid v1.2.0
    google.golang.org/appengine v1.6.1 // indirect
)
Copy after login
1.4.2 replace Each package accessing golang.org/x in China needs to bypass the firewall. You can go.mod Use replace to replace it with the corresponding library on github.
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7
gopkg.in/vmihailenco/msgpack.v2 v2.9.1
gopkg.in/yaml.v2 <=v2.2.1
github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e
latest
Copy after login
1.5 go get

Execute the

go get

command in the project to download the dependency package, and you can also specify the downloaded version.

Run

go get -u

will upgrade to the latest minor version or revision (x.y.z, z is the revision number, y is the minor version number)Run

go get -u=patch
    will be upgraded to the latest revision
  1. Run go get package@version
  2. will be upgraded to the specified version No. version
  3. If you download all dependencies, you can use the
  4. go mod download
  5. command. 1.6 Organizing dependencies
After we delete the dependent code in the code, the related dependent libraries will not be automatically removed in the

go.mod file. In this case, we can use the go mod tidy

command to update the dependencies in

go.mod

.

1.7 go mod editFormattingBecause we can manually modify the go.mod file, sometimes we need to format the file. Go provides the following commands:

replace (
    golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac => github.com/golang/crypto v0.0.0-20180820150726-614d502a4dac
    golang.org/x/net v0.0.0-20180821023952-922f4815f713 => github.com/golang/net v0.0.0-20180826012351-8a410e7b638d
    golang.org/x/text v0.3.0 => github.com/golang/text v0.3.0
)
Copy after login
Add dependencies

go mod edit -fmt
Copy after login

Remove dependencies

If you just want to modify the contents of the

go.mod

file , then you can run

go mod edit -droprequire=package path

. For example, if you want to remove the

golang.org/x/text

package in

go.mod, you can Use the following command:

go mod edit -require=golang.org/x/text
Copy after login
More usage of go mod edit can be viewed through go help mod edit. 1.8 Using go module in the project

1.8.1 Existing projectIf you need to enable go module

for an existing project, you can Follow the steps below:

Execute

go mod init

in the project directory to generate a go.mod file.

    Execute
  1. go get to find and record the dependencies of the current project, and generate a go.sum to record the version and hash value of each dependent library.
  2. 1.8.2 New ProjectFor a newly created project, we can follow the following steps under the project folder:<ol> <li>执行<code>go mod init 项目名命令,在当前项目文件夹下创建一个go.mod文件。
  3. 手动编辑go.mod中的require依赖项或执行go get自动发现、维护依赖。

二、包和调用文件在同一项目下

例如:

moduledemo
├── go.mod
├── main.go
└── mypackage
    └── mypackage.go  // package mp 定义包名为 mp
Copy after login

步骤:

1.在项目下创建一个 go.mod 文件,文件名只能为这个。

2.在 go.mod 文件中添加以下代码

module moduledemo  // 设定 moduledemo 为包根目录名,可以随意改变该名字,只需要导入时一致就好
go 1.14  // 表明版本
Copy after login

3.导入想要的包文件

import "moduledemo/mypackage"  // 这里是导入包目录下的包文件名
Copy after login

4.使用包文件

mp.MyPackage()  // 使用包中的 MyPackage() 函数
Copy after login

三、包和被调用文件不在同一个项目下

例如:

├── moduledemo
│   ├── go.mod
│   └── main.go
└── mypackage
    ├── go.mod
    └── mypackage.go  // package mp 定义包名为 mp
Copy after login

步骤

1.在 mypackage 下面创建 go.mod 文件,并添加以下代码

module mypackage

go 1.14
Copy after login

2.在 moduledemo 下面创建 go.mod 文件,并添加以下代码

module moduledemo

go 1.14


require mypackage v0.0.0  // 这个会在你执行 go build 之后自动在该文件添加
replace mypackage => ../mypackage  // 指定需要的包目录去后面这个路径中寻找
Copy after login

3.导入和使用

import "mypackage"  // 因为该包目录本身就是包文件所以无需添加下一级路径

mp.MyPackage()  // 使用包中的 MyPackage() 函数
Copy after login

The above is the detailed content of What is go module? how to use? (with usage examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template