package's type cannot be used as the vendored

2019-01-17 21:22发布

问题:

I'm trying to use this Golang Yelp API package. In some of its structs, it uses types defined in guregu's null package.

I want to declare a struct defined in the Yelp API package, where some of its fields have null.Float as a value (i.e. this struct, which im trying to use). So in my program, I import both the Yelp API package and guregu's null package and try to declare the struct, with ip.Lat and ip.Lat being 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         }

But when I run the program, it tells me:

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

I tried 2 things:

1) I did not import the null package, which caused Go to complain about null being undefined. 2) I also tried importing the vendored package directly, which caused Go to tell me use of vendored package not allowed.

Any Ideas on how to fix this?

回答1:

The solution here seems to be that the library I'm trying to use needs to be reworked to prevent this kind of thing from happening.

The two possible ways to change the library seem to be

1) not vendor at all - this works if the dependency does not need to be a specific version.

2) vendored, but do not expose the vendored library to the public. Create some wrapper functions in the library so that people can create the types indirectly.

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



回答2:

I had the same issue. As a work around, I deleted the associated package's vendor folder and moved their content to my $GOPATH folder.

Source of answer: https://github.com/prometheus/prometheus/issues/1720



回答3:

Just had a similar issue. Putting both libraries in /vendor resolved. Using govendor get xxxx



回答4:

Had a similar issue while using Godep and I resolved by deleting /vendor and re-running godep save ./... - Hope it helps.



标签: go struct