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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
G
Google Developers Blog
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
博客园 - 叶小钗
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
P
Palo Alto Networks Blog
C
Cisco Blogs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog
Security Latest
Security Latest
H
Heimdal Security Blog
小众软件
小众软件
Cyberwarzone
Cyberwarzone
The Hacker News
The Hacker News
P
Privacy International News Feed
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
Scott Helme
Scott Helme
博客园 - 【当耐特】
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
Jina AI
Jina AI
Spread Privacy
Spread Privacy
量子位
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网

陈少文的网站

巨变与机遇的未来十年 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) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
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. 参考


微信公众号