Golang自定义域名包的使用方法

Golang自定义域名包的使用方法 大家好。

当我为我的包创建自定义域名,例如这样时:

<meta name="go-import" content="code.gyt.is/writefreely git https://github.com/gytisrepecka/writefreely" />
<meta name="go-source" content="code.gyt.is/writefreely https://github.com/gytisrepecka/writefreely https://github.com/gytisrepecka/writefreely/tree/master{/dir} https://github.com/gytisrepecka/writefreely/blob/master{/dir}/{file}#L{line}" />

然后我在新项目中尝试执行 go get 时,遇到了如下错误:

go get code.gyt.is/writefreely                                                                                                                                                  ─╯
go: downloading code.gyt.is/writefreely v0.0.1
go: code.gyt.is/writefreely@v0.0.1: verifying module: code.gyt.is/writefreely@v0.0.1: reading https://goproxy.cn/sumdb/sum.golang.org/lookup/code.gyt.is/writefreely@v0.0.1: 404 Not Found
        server response:
        not found: code.gyt.is/writefreely@v0.0.1: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/68810a2e811d4b05ff603d5d5704409f3cd5eacd82dd5d2038f869ef899160dc: exit status 128:
                fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled
        Confirm the import path was entered correctly.
        If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

我的配置哪里出错了? 请帮忙。 谢谢。


更多关于Golang自定义域名包的使用方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

你好,

我有两点观察:

  1. 我看到的第一个错误是在查询中国 Go 模块校验和数据库时出现 404 未找到:

    verifying module: code.gyt.is/writefreely@v0.0.1: reading https://goproxy.cn/sumdb/sum.golang.org/lookup/code.gyt.is/writefreely@v0.0.1: 404 Not Found

    随后的错误信息显示,位于 goproxy.cn 的 Go 校验和数据库试图访问 gitlab.com

    server response:
            not found: code.gyt.is/writefreely@v0.0.1: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/68810a2e811d4b05ff603d5d5704409f3cd5eacd82dd5d2038f869ef899160dc: exit status 128:
                    fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled
            
    

    我不明白为什么当导入路径解析到 GitHub 时,代理会去操作 GitLab。但无论如何,v0.0.1 这个版本似乎根本不存在。(我尝试了 go get code.gyt.is/writefreely@v0.0.1 并得到了一个无效版本错误。)也许可以尝试使用另一个 Go 代理。

  2. 当我运行 go get 命令时,我得到了一个不同的错误(针对另一个版本):

    > go get code.gyt.is/writefreely                                                                                                                                                       0s
    go: downloading code.gyt.is/writefreely v0.13.2
    go: code.gyt.is/writefreely@upgrade (v0.13.2) requires code.gyt.is/writefreely@v0.13.2: parsing go.mod:
    	module declares its path as: github.com/writefreely/writefreely
    	        but was required as: code.gyt.is/writefreely
    

    这个问题应该很容易修复:将你的 go.mod 文件中的模块路径替换为自定义导入路径:

    module code.gyt.is/writefreely
    

    更改之后,你可能需要提升版本号,以便更新代理。

更多关于Golang自定义域名包的使用方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


根据错误信息,问题在于 Go 工具链无法从你的自定义域名正确获取 Git 仓库。错误显示 fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled,这表明 Go 尝试从 gitlab.com 拉取代码,但你的 go-import 元标签指向的是 GitHub。

以下是解决方案:

  1. 检查 go-import 元标签:确保你的自定义域名服务器返回的 HTML 中包含正确的元标签。对于你的示例,应该指向 GitHub 而非 GitLab。你的元标签看起来正确,但需要确认服务器实际返回的内容。

  2. 验证服务器响应:使用 curl 检查你的自定义域名是否返回正确的元标签。例如:

    curl -s https://code.gyt.is/writefreely?go-get=1 | grep go-import
    

    预期输出应包含:

    <meta name="go-import" content="code.gyt.is/writefreely git https://github.com/gytisrepecka/writefreely">
    

    如果输出不同,请修正服务器配置。

  3. 检查 Git 仓库权限:错误提到 GitLab,但你的元标签指向 GitHub。这可能是因为:

    • 你的本地 Git 配置有误,导致重定向到 GitLab。
    • 仓库可能设置了重定向或镜像到 GitLab。 确保 GitHub 仓库 https://github.com/gytisrepecka/writefreely 是公开可访问的。如果是私有仓库,需要配置 Git 凭证。对于私有仓库,设置 GOPRIVATE 环境变量:
    export GOPRIVATE=code.gyt.is
    

    然后使用 git config 配置凭证助手,例如:

    git config --global credential.helper store
    

    之后执行一次 Git 操作(如 git clone)来存储凭证。

  4. 清除 Go 模块缓存:有时缓存会导致问题,清除缓存后重试:

    go clean -modcache
    rm -rf ~/go/pkg/mod/cache/vcs/*
    

    然后再次运行 go get

  5. 完整示例:以下是一个有效的自定义域名服务器配置示例(使用 Go 的 net/http 包):

    package main
    
    import (
        "fmt"
        "net/http"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
        if r.URL.Query().Get("go-get") == "1" {
            html := `<html>
    <head>
        <meta name="go-import" content="code.gyt.is/writefreely git https://github.com/gytisrepecka/writefreely">
        <meta name="go-source" content="code.gyt.is/writefreely https://github.com/gytisrepecka/writefreely https://github.com/gytisrepecka/writefreely/tree/master{/dir} https://github.com/gytisrepecka/writefreely/blob/master{/dir}/{file}#L{line}">
    </head>
    <body>
        go get code.gyt.is/writefreely
    </body>
    </html>`
            fmt.Fprint(w, html)
            return
        }
        http.NotFound(w, r)
    }
    
    func main() {
        http.HandleFunc("/writefreely", handler)
        http.ListenAndServe(":80", nil)
    }
    

    确保服务器运行在 code.gyt.is 并响应路径 /writefreely

  6. 测试 go get:配置完成后,在新项目中运行:

    go get code.gyt.is/writefreely
    

    如果问题仍然存在,检查网络代理(如 goproxy.cn)是否支持自定义域名。可以临时禁用代理测试:

    export GOPROXY=direct
    go get code.gyt.is/writefreely
    

根据错误信息,最可能的原因是 Git 仓库权限或元标签配置不匹配。请优先验证服务器返回的元标签是否正确,并确保 GitHub 仓库可公开访问。

回到顶部