Skip to content
On this page

golang 实现私有代理

通过自定义域名上的 go-import 元数据,让 go get 把模块路径解析到私有 Git 仓库。下文以 liuxiaobo.site/golib 为例,将 liuxiaobo.site 换成你的域名即可。

自定义域名

在可访问该域名的 Web 根路径提供如下 HTML(或等价路由返回相同 <head>):

html
<!doctype html>
<html>
  <head>
    <meta
      name="go-import"
      content="liuxiaobo.site/golib git https://github.com/liuxiaobopro/go-crate"
    />
  </head>
  <body>
    golib
  </body>
</html>

设置环境变量

bash
go env -w GOPRIVATE=liuxiaobo.site/*
go env -w GONOSUMDB=liuxiaobo.site/*
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
git config --global url."git@github.com:".insteadOf "https://github.com/"

生成密钥

bash
ssh-keygen -t rsa

查看公钥

bash
cat ~/.ssh/id_rsa.pub

将公钥添加到 GitHub(或对应 Git 托管)账户的 SSH keys 后,即可用 SSH 拉取私有模块。

技术文库