【问题标题】:package's type cannot be used as the vendored package's type包的类型不能用作供应商包的类型
【发布时间】:2023-04-10 17:37:02
【问题描述】:

我正在尝试使用这个Golang Yelp API package。在它的一些结构中,它使用guregu's null package 中定义的类型。

我想声明一个在 Yelp API 包中定义的结构,其中的一些字段具有 null.Float 作为值 (i.e. this struct, which im trying to use)。所以在我的程序中,我同时导入了 Yelp API 包和 guregu 的 null 包,并尝试声明结构,ip.Lat 和 ip.Lat 是 float64s。 (null.FloatFrom definition):

 33         locationOptions := yelp.LocationOptions{
 34                 ip.Zip,
 35                 &yelp.CoordinateOptions{
 36                         Latitude: null.FloatFrom(ip.Lat),
 37                         Longitude: null.FloatFrom(ip.Lon),
 38                 },
 39         }

但是当我运行程序时,它告诉我:

./cli.go:36: cannot use "github.com/guregu/null".FloatFrom(ip.Lat) (type
"github.com/guregu/null".Float) as type "github.com/JustinBeckwith/go-
yelp/yelp/vendor/github.com/guregu/null".Float in field value

我尝试了两件事:

1) 我没有导入 null 包,导致 Go 抱怨 null 未定义。 2)我也试过直接导入vendored包,导致Go告诉我use of vendored package not allowed

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: go struct


    【解决方案1】:

    这里的解决方案似乎是我正在尝试使用的库需要重新设计以防止这种事情发生。

    改变库的两种可能方式似乎是

    1) 根本不是供应商 - 如果依赖项不需要是特定版本,则此方法有效。

    2) vendored,但不要向公众公开 vendored 库。在库中创建一些包装函数,以便人们可以间接创建类型。

    this discussion about vendoring on reddit for more ideas/reasons why.

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。作为一种解决方法,我删除了相关包的供应商文件夹并将其内容移动到我的 $GOPATH 文件夹中。

      答案来源:https://github.com/prometheus/prometheus/issues/1720

      【讨论】:

        【解决方案3】:

        刚刚遇到类似的问题。将两个库都放入/vendor 已解决。使用govendor get xxxx

        【讨论】:

          【解决方案4】:

          在使用 Godep 时遇到了类似问题,我通过删除 /vendor 并重新运行 godep save ./... 解决了 - 希望对您有所帮助。

          【讨论】:

            猜你喜欢
            • 2021-05-05
            • 1970-01-01
            • 2017-04-27
            • 2017-08-12
            • 1970-01-01
            • 1970-01-01
            • 2022-07-12
            • 2016-10-12
            相关资源
            最近更新 更多