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

相关文章:

c++ - 将结构的值设置为五个可能值之一

c - 如何在 C 中的结构中初始化结构?

c++ - 在 C++、结构或类中应该使用什么来创建链接列表

c - 为什么访问结构时会出现段错误?

go - 在 Go 语言中,函数末尾缺少 return

go - 如何使用 golang 在 Firebase 中创建用户?

arrays - 理解 Go 的 slice 符号

struct - 你能把 struct impl block 放在不同的文件中吗?

json - 在 Golang 中将多个 JSON 字段解码为一个

go - 如何隐藏日志