惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Jina AI
Jina AI
罗磊的独立博客
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
J
Java Code Geeks
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
月光博客
月光博客
博客园_首页
Vercel News
Vercel News
P
Proofpoint News Feed
GbyAI
GbyAI
Y
Y Combinator Blog

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19)
Go 私有包的构建和使用
微信公众号 · 2021-06-24 · via 陈少文的网站

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()
}
  • 下载依赖到 vendor
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 替换 https

如果私有仓库使用的是 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. 依赖的包,正在开发,尚未发布
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>
  • 将项目绑定到域名: go.gianarb.it

  • 使用 go get go.gianarb.it/irc 下载依赖包

5. 常见问题

  • go mody tidy 时,SUM 校验错误
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. 参考


微信公众号