【问题标题】:Is there a metadata database for Go modules? [closed]Go 模块有元数据数据库吗? [关闭]
【发布时间】:2020-11-12 16:41:54
【问题描述】:

与 npm 一样,npm 中的所有包都有一个索引文件 - https://replicate.npmjs.com/_all_docs。我的问题是Go中也有这样的索引文件吗?

【问题讨论】:

  • 资源请求在 StackOverflow 上是题外话。但除此之外,你想要这个列表 for 做什么?也许这里隐藏了一个主题问题......

标签: database go package repository metadata


【解决方案1】:

https://index.golang.org/index 的模块索引是通过https://proxy.golang.org 获取的新模块版本的提要(两者的文档都是here)。它是可用的最全面的索引,但它肯定不完整,因为 proxy.golang.org 的使用是可选的。

您可以通过将 since 查询参数设置为前一个块中的最大时间戳来读取 proxy.golang.org 看到的所有模块版本,以 2K 块为单位。

使用以下代码阅读整个提要:

package main

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

type module struct {
    Path, Version, Timestamp string
}

func chunk(since string) ([]module, error) {
    resp, err := http.Get("https://index.golang.org/index?since=" + since)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    dec := json.NewDecoder(resp.Body)
    var mods []module
    for dec.More() {
        var mod module
        if err := dec.Decode(&mod); err != nil {
            return nil, err
        }
        mods = append(mods, mod)
    }
    return mods, nil
}

func main() {
    var lastMod module
    for {
        mods, err := chunk(lastMod.Timestamp)
        if err != nil {
            log.Fatal(err)
        }

        // We are done of there are no results.
        if len(mods) == 0 {
            return
        }

        // Last module in previous chunk can be returned in this chunk.
        if mods[0] == lastMod {
            mods = mods[1:]
        }

        // We are done of there are no results.
        if len(mods) == 0 {
            return
        }

        for _, m := range mods {
            fmt.Println(m.Path, m.Version)
        }

        lastMod = mods[len(mods)-1]
    }
}

【讨论】:

    【解决方案2】:

    据我所知,Go.dev 提供了一个索引文件,Go Module Index,但我不确定它是否包含所有 go 模块。

    【讨论】:

    • 我认为 Go Module 索引并没有包含所有的模块,比这个生态系统的总数要少得多。
    • 谢谢你,velkor!你知道这个服务的提供者是谁吗,我如何收集 Golang 的所有元数据?
    • “我不确定它是否包含所有的 go 模块。” -- 它显然不能包含 all Go 模块。充其量,它可以包含它知道的公共 Go 模块,因为它是通过代理请求的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2015-06-11
    相关资源
    最近更新 更多