14-Go Modules

go.mod 简介

go mod init abc 初始化命令会在当前目录下生成 go.mod 文件,在一个项目中,此文件只需要一份,放置在项目根目录。

go.mod 格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 项目名,别人引入你这个项目的路径
module github.com/a/b

// 当前环境的版本号
go 1.16

// 依赖
// 出现 indirect 表示项目中还未使用
require (
one/thing v1.3.2
other/thing v2.5.0 // indirect
...
)

// 排除具体的版本
// 如果某个版本出现重要bug,可以这么做
exclude (
bad/thing v0.7.3
)

// 替代
replace (
// 项目中通过src/thing引入,但实际引入的位置是dst/thing,相当于取个别名
src/thing 1.0.2 => dst/thing v1.1.0
)

14-Go Modules
https://flepeng.github.io/021-Go-11-安装和配置-Go-Modules-之-go-mod/
作者
Lepeng
发布于
2024年11月11日
许可协议