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/55159029/

相关文章:

json - 如何从 golang 中的 json 访问键和值?

c++ - 动态创建、命名和放置队列

iOS - 用于跟踪全局设置的持久变量

结构体中的 char 数组成员 - 值复制到两个成员

go - 编辑内存中的zip文件并通过http响应返回它会导致文件损坏

android - 使用 go-gcm 发送 XMPP 推送通知时正确的错误处理?

json - 如何使用 html 模板解码结构?

c++ - 带有构造函数的结构 vector

mongodb - Golang MGO 模块是否锁定或解锁 Go 对象?

go - 去 slice 突变最佳实践