go-get私有库

https://static.ffeeii.com/ffeeii.com/2022/03/golang.png

相比Java最强大的包管理工具maven,PHP的composer也非常方便, 而golang 1.11 新加了go modules 特性,终于在包管理上面前进了一大步。本文配合Go mod搭建一个私有化共享包。

1、用Gitlab或者gitee创建1个库,这里以阿里云效的为参考 https://codeup.aliyun.com/xx1/xx2

2、代码库目录初始化mod

go mod init codeup.aliyun.com/xx1/xx2

最终go.mod中生成了module名称和go的版本

module codeup.aliyun.com/xx1/xx2
go 1.17

3、设置环境变量 GOPRIVATE

可以修改ssh 配置文件.zprofile或者.profile添加如下

export GOPRIVATE=codeup.aliyun.com

或者直接命令行添加

go env -w GOPRIVATE=codeup.aliyun.com

4、配置 ~/.gitconfig,因为 go get 走的https协议,需要将https转换为ssh

[url "ssh://git@codeup.aliyun.com"]
    insteadOf = https://codeup.aliyun.com

5、准备就绪后,在项目代码中使用

go get codeup.aliyun.com/xx1/xx2

其他主要事项

  • 实际用的过程中,如果代码库路径有3段,可能会匹配不上,建议用2端
  • GO111MODULE=on