Golang代理无法获取某些包数据的问题

Golang代理无法获取某些包数据的问题 我正在尝试获取一些关于Go语言包的元数据以及包本身。 我发现对于某些包,Go代理不提供数据,例如:

https://proxy.golang.org/github.com/eclipse-kanto/suite-connector/@v/v0.1.0-M3.info 以及将后缀 .info 替换为 .zip 的相同URL。

该端点返回:错误请求:无效的转义版本 “v0.1.0-M3”。

但我可以看到GitHub上存在这样的标签: https://github.com/eclipse-kanto/suite-connector/releases/tag/v0.1.0-M3

请问您能帮助解决这个问题,或者建议其他方法来实现我的目标吗?

谢谢大家!


更多关于Golang代理无法获取某些包数据的问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html

7 回复

看到预发布标识符的转换如何影响URL并解决了问题,这很有趣。

更多关于Golang代理无法获取某些包数据的问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


说得好,@NobbZ。那么这听起来像是一个 bug。不过,我快速搜索了 GitHub - golang/go: Go 编程语言 上的 issues,没有发现任何相关的内容。

这甚至违反了语义化版本规范(semver)的规定,尽管规范没有具体说明应遵循哪个版本,或许早期版本的限制更为严格。

已发布的模块会附带一个遵循语义化版本模型的版本号。

语义化版本规范允许使用任意的字母数字标签,且未指定特定的大小写规则。

参见 Semantic Versioning 2.0.0 | Semantic Versioning

非常感谢你们的评论!

我找到了问题所在。为了获取信息端点的确切 URL,你可以运行以下命令:go mod download -json <package_name>@<package_version>

你将获得一组关于包元数据的 URL。

因此,对于上述情况,大写字母应转换为 <大写字母>!<小写字母>

你也可以在这里找到规范:https://github.com/golang/go/blob/c54bc3448390d4ae4495d6d2c03c9dd4111b08f1/src/cmd/go/internal/module/module.go#L359-L417

请告诉我这对你是否有效, 谢谢。

这很有趣,感谢你的提示!

所以你的初始URL只需要进行这个转换:M → !m

https://proxy.golang.org/github.com/eclipse-kanto/suite-connector/@v/v0.1.0-!m3.info

…然后我得到了一个有效的响应。

{
  "Version": "v0.1.0-M3",
  "Time": "2023-05-19T07:04:23Z",
  "Origin": {
    "VCS": "git",
    "URL": "https://github.com/eclipse-kanto/suite-connector",
    "Ref": "refs/tags/v0.1.0-M3",
    "Hash": "8ecca4f93d4b691761a5cdd3fcb1a48e07cc029d"
  }
}

你好 @ozblumen,欢迎来到论坛。

看起来预发布限定符必须遵循特定的模式(尽管这一点在文档中并未说明)。当我尝试访问一个(不存在的)“beta.1”预发布版本时…

https://proxy.golang.org/github.com/eclipse-kanto/suite-connector/@v/v0.1.0-beta.1.info

…出现的错误信息与你的不同:

not found: github.com/eclipse-kanto/suite-connector@v0.1.0-beta.1: invalid version: unknown revision v0.1.0-beta.1

(这个错误是合理的,因为 beta.1 并不存在。)

我尝试了多种变体,发现预发布标识符不能包含大写字母。(例如,ALPHAAlphaalphA 会触发“无效的转义版本”错误,而 alpha 则工作正常,并触发了预期的“未找到”错误。)

我不确定这是一个错误还是有意为之。

这个问题是由于Go模块代理对版本命名的限制导致的。Go代理要求版本号遵循语义化版本规范,而v0.1.0-M3中的-M3部分不符合标准语义化版本格式。

根据Go模块的版本规范,预发布标签应该使用点号分隔,例如v0.1.0-M.3v0.1.0-pre.3。当前版本v0.1.0-M3中的-M3被解析为无效的转义版本。

要解决这个问题,你可以考虑以下几种方法:

  1. 使用replace指令(如果你的go.mod中需要这个包):
module your-module

go 1.19

require github.com/eclipse-kanto/suite-connector v0.0.0

replace github.com/eclipse-kanto/suite-connector => github.com/eclipse-kanto/suite-connector v0.1.0-M3
  1. 直接使用Git引用
go get github.com/eclipse-kanto/suite-connector@v0.1.0-M3
  1. 手动下载并安装
# 克隆仓库
git clone https://github.com/eclipse-kanto/suite-connector.git
cd suite-connector
git checkout v0.1.0-M3

# 在项目中使用本地路径
go mod edit -replace github.com/eclipse-kanto/suite-connector=../suite-connector
  1. 使用GOPROXY=direct绕过代理
GOPROXY=direct go get github.com/eclipse-kanto/suite-connector@v0.1.0-M3
  1. 如果只是需要获取元数据,可以使用GitHub API
package main

import (
    "encoding/json"
    "fmt"
    "io"
    "net/http"
)

func main() {
    // 获取release信息
    resp, err := http.Get("https://api.github.com/repos/eclipse-kanto/suite-connector/releases/tags/v0.1.0-M3")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    
    body, _ := io.ReadAll(resp.Body)
    var release map[string]interface{}
    json.Unmarshal(body, &release)
    
    fmt.Printf("Release: %v\n", release["name"])
}

对于包下载,你可以直接使用GitHub的归档链接:

# 下载特定版本的zip包
curl -L https://github.com/eclipse-kanto/suite-connector/archive/refs/tags/v0.1.0-M3.zip -o suite-connector.zip

这个问题本质上是版本命名规范的问题,建议联系该包的维护者,建议他们使用符合Go模块规范的版本标签格式,如v0.1.0-M.3v0.1.0-pre.3

回到顶部