利用 gopkgs 自定义 Go 库的导入路径

本页内容

自定义 Go 模块的导入路径有一个主要的好处,当你打算更换代码的托管服务器,比如 GitHub 换到 GitLab、Gitee 时,你不需要更改包的导入路径。而 gopkgs 正是这样一个 HTTP 服务,可以让你自定义 Go 库的导入路径。

gopkgs.net 是笔者为了自身业务搭建的,大家可以免费试用。

先决条件

  • 域名

步骤

本文以 clevergo.tech 作为例子,其代码托管在 GitHub,比如:

Import PathRepository
clevergo.tech/clevergohttps://github.com/clevergo/clevergo
clevergo.tech/captchashttps://github.com/clevergo/captchas
  1. 注册账号并登录后台。
  2. 然后在后台添加域名,并添加相应的 CNAME 记录到 gopkgs.net,然后验证域名所有权。
  3. 接着添加 Go 包,填写相应的信息即可。

验证

验证时,我们需要使用 go get -u -v,比如:

1$ go get -u -v clevergo.tech/clevergo
2get "clevergo.tech/clevergo": found meta tag get.metaImport{Prefix:"clevergo.tech/clevergo", VCS:"git", RepoRoot:"https://github.com/clevergo/clevergo"} at //clevergo.tech/clevergo?go-get=1
3clevergo.tech/clevergo (download)
4get "clevergo.tech/log": found meta tag get.metaImport{Prefix:"clevergo.tech/log", VCS:"git", RepoRoot:"https://github.com/clevergo/log"} at //clevergo.tech/log?go-get=1
5clevergo.tech/log (download)
6clevergo.tech/log
7clevergo.tech/clevergo

如上所示,解析的 META 标签正确,即表示成功。