go - 包的类型不能用作 vendored 包的类型

标签 go struct

我正在尝试使用这个 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

关于如何解决这个问题有什么想法吗?

最佳答案

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

两种可能的改库方法好像是

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

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

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

关于go - 包的类型不能用作 vendored 包的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58055383/

相关文章:

c++ - 使用 VS2008 编译的 C/C++ 中的结构复制问题

在结构中创建指向数组的指针

matlab - Matlab 中结构内的元胞数组 - 可能吗?

templates - Golang 新模板不起作用

go - Go 例程后关闭冗余 sql.Rows 对象的推荐方法

generics - 创建由通用NewType索引的通用结构

C 结构体指针问题

go - 执行 UnmarshalExtJSON 时读取数组的无效请求

go - 在 Go 中使用 WaitGroup 和 channel

go - 尝试包装函数时函数用作值错误