Go 私有包的构建和使用
微信公众号
·
2021-06-24
·
via 陈少文的网站
陈少文的网站 Posts Go 私有包的构建和使用 Please enable Javascript to view the contents
1. 创建一个 Go Modules 项目 1
2
mkdir go-test
cd go-test
1
2
3
4
5
go mod init gitlab.private.com/shaowenchen/go-test
go: creating new go.mod: module gitlab.private.com/shaowenchen/go-test
go: to add module requirements and sums:
go mod tidy
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main
import (
"github.com/gin-gonic/gin"
)
func main () {
r := gin . Default ()
r . GET ( "/" , func ( c * gin . Context ) {
c . JSON ( 200 , gin . H {
"message" : "hello world." ,
})
})
r . Run ()
}
1
2
go mod tidy
go mod vendor
1
2
3
4
5
go run main.go
[ GIN-debug] GET / --> main.main.func1 ( 3 handlers)
[ GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[ GIN-debug] Listening and serving HTTP on :8080
1
2
3
4
5
git init
git remote add origin git@gitlab.private.com:shaowenchen/go-test.git
git add .
git commit -m "Initial commit"
git push -u origin master
2. 如何拉取私有依赖包 如果私有仓库使用的是 SSH 鉴权,那么需要将 http/https 替换为 git@ 的形式。
1
git config --global url."git@gitlab.private.com:" .insteadof "https://gitlab.private.com/"
或者修改 ~/.gitconfig 添加如下内容:
[url "git@gitlab.private.com:"]
insteadof = https://gitlab.private.com/
Go Modules 默认使用代理去更新依赖,需要对私有仓库依赖包进行豁免。同时,没有 GOSUMDB 服务对私有依赖包进行校验,因此也需要豁免。
1
2
3
go env -w GOPRIVATE = "gitlab.private.com"
go env -w GONOPROXY = "gitlab.private.com"
go env -w GONOSUMDB = "gitlab.private.com"
如果代码仓库服务器没有使用合法的证书,还需要配置如下环境变量:
1
go env -w GOINSECURE = "gitlab.private.com"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main
import (
"github.com/gin-gonic/gin"
"gitlab.private.com/share/log"
)
func main () {
r := gin . Default ()
r . GET ( "/" , func ( c * gin . Context ) {
c . JSON ( 200 , gin . H {
"message" : "hello world." ,
})
})
log . Info ( "hello world" )
r . Run ()
}
1
2
3
go get "gitlab.private.com/share/log"
go mod tidy
go mod vendor
1
2
go run main.go
go build
3. 替换无法下载的包 通常有两种情况会使用 replace 替代包:
无法直接拉取,需要使用其他来源的镜像包 依赖的包,正在开发,尚未发布 1
2
3
4
5
6
7
8
go 1.16
require (
github.com/gin-gonic/gin v1.7.2
gitlab.private.com/share/log v0.0.4
)
replace gitlab.private.com/share/log v0.0.4 = > /module/path/log
或者
go mod edit -replace=gitlab.private.com/share/log@v0.0.4=/module/path/log
4. 如何自定义包的域名地址 通常添加依赖包的格式是 github.com/gin-gonic/gin,其中 github.com 就是代码服务器地址,gin-gonic 是组织名,gin 是项目名。
但是,Kuberntes 中的包并不是 github.com/kubernetes/kubernetes ,而是 k8s.io/kubernetes,那么这是怎么实现的呢?
这里需要对 k8s.io 进行重定向,以 github.com/gianarb/go-irc 替换为 go.gianarb.it/irc 使用 GitHub Pages 为例:
创建一个项目 go-libraries 添加一个与项目同名的文件, irc , 内容如下: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< html >
< head >
< meta
name = "go-import"
content = "go.gianarb.it/irc git https://github.com/gianarb/go-irc"
/>
< meta
http-equiv = "refresh"
content = "0;URL='https://github.com/gianarb/go-irc'"
/>
</ head >
< body >
Redirecting you to the
< a href = "https://github.com/gianarb/go-irc" > project page</ a > ...
</ body >
</ html >
5. 常见问题 1
2
3
verifying gitlab.private.com/mygroup/proto@v0.0.1: checksum mismatch
downloaded: h1:zpwTvQGgQFudfxFgnj9w90do3ps+BQ9ir/Sa7oVPooA=
go.sum: h1:+XAvplGdXmvEank7sOI+Cd3GYdq3dBEDpW4/DO3sSUw=
解决办法:
1
2
3
go clean -modcache
rm go.sum
go mod tidy
6. 参考
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。