与你常在

参考

创建模块

1
2
3
4
#首先在src路径下创建一个模块的文件夹,编写模块内容
#之后使用gomod初始化
$go mod init github.com/haillk/testmod
go: creating new go.mod: module github.com/haillk/testmod

这会在该包目录西安创建一个名为go.mod的文件,内容如下

1
module github.com/haillk/testmod

之后将该包上传到github即可。这就使我们的包变成了一个模块。

上传和tag操作
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
git init
git add *
git commit -am "first commit"
git push -u origin master

git tag v1.0.0
git push --tags

git checkout -b v1
git push -u origin v1

使用模块

在文件中引用刚刚上传的包

1
2
3
...
import "github.com/haillk/testmod"
...

使用时也要先初始化

1
go mod init test2

该步会创建go.mod文件

之后使用build命令构建

1
2
3
go build
go: finding github.com/haillk/testmod v1.0.0
go: downloading github.com/haillk/testmod v1.0.0

包会被下载到pkg/mod/github.com/haillk文件夹中去。

同时项目中出现新的文件go.sum

更新版本

1
go get github.com/haillk/testmod@v1.0.1