示例:go get https://lvtao.net/fro
要实现上面的效果,不一定要将域名解析为一个git仓库,可以直接使用html配合github仓库实现
<meta name="go-import" content="lvtao.net/fro git https://github.com/lvtao-net/fro">
使用html元数据,即可轻松实现,它的模式是这样的<meta name="go-import" content="import-prefix vcs repo-root">
实际上,content属性中的import-prefix的位置上应该填入我们自定义的远程代码包导入路径的前缀。这个前缀应该与我们的处理程序关联的那个路径相一致。而vsc显然应该代表与版本控制系统有关的标识。还记得表0-2中的主命令列吗?这里的填入内容就应该该列中的某一项。在这里,由于fro项目使用的是Git,所以这里应该填入git。至于repo-root,它应该是与该处理程序关联的路径对应的Github网站的URL
由于go get采用https协议所以处理请求的这个地址必须是https,一个完整的请求html如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="go-import" content="lvtao.net/fro git https://github.com/lvtao-net/fro.git">
    <meta name="go-source" content="lvtao.net/fro">
    <meta http-equiv="refresh" content="0; url=https://godoc.org/lvtao.net/fro">
</head>
<body >
 <a href="https://godoc.org/lvtao.net/fro">see the package on godoc</a>
</body>
</html>

这样使用go get自己的域名就行了,如果没有这个meta标签直接跳转,go mod验证不会通过的

其中go-source中的配置有两种方式,第一种是直接引入lvtao.net/fro,第二种则是会引入版本及文件,像lvtao.net/fro/v2/pkg/array类似这样的

<meta name="go-source" content="lvtao.net/fro _ https://github.com/lvtao-net/fro/tree/main{/dir} https://github.com/lvtao-net/fro/blob/main{/dir}/{file}#L{line}">

<meta name="go-source" content="lvtao.net/fro/v2 _ https://github.com/lvtao-net/fro/tree/v2{/dir} https://github.com/lvtao-net/fro/blob/v2{/dir}/{file}#L{line}">

既然 Golang 包是通过 html 跳转的,显然 git 直接设置 remote 为域名就不行了
幸运的是,我们一般是用的 .git 这样作为 remote,所以就可以在 nginx 上用正则表达式来匹配.git,例如

location ~* \.(git|git/) {
    return 307 https://github.com/lvtao-net$request_uri;
}

这样 remote 设置为https://lvtao.net/nps.git就能正常使用github的仓库了

另外附一个能用的nginx配置规则,当然这个反向的是自己的git仓库,而非github了 你可以自己做对应修改

location / {
    if ($query_string ~ "go-get=1") {
        add_header Content-Type 'text/html; charset=utf-8';
        return 200 '<html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="go-import" content="$host$uri git https://$host$uri.git">
            <meta name="go-source" content="$host$uri _ https://$host$uri/src/branch/master{/dir} https://$host$uri/src/branch/master{/dir}/{file}#L{line}">
            </head>
            <body>
            Install command: <br/>
            <code>go get $host$uri</code> <br/><br/>
            Import in source code:<br/>
            <code>import "$host$uri"</code><br/><br/>
            GoDoc: https://pkg.go.dev/$host$uri
            </body>
            </html>';
    }
    proxy_pass http://127.0.0.1:3000;
}