Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

package's type cannot be used as the vendored package's type

Tags:

struct

go

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?

like image 357
Julien Chien Avatar asked Jun 29 '16 06:06

Julien Chien


4 Answers

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

like image 36
Randy Yuan Avatar answered Nov 01 '22 12:11

Randy Yuan


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.

like image 152
Julien Chien Avatar answered Nov 01 '22 14:11

Julien Chien


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

like image 4
Viktor Yarmak Avatar answered Nov 01 '22 14:11

Viktor Yarmak


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

like image 1
Aldo 'xoen' Giambelluca Avatar answered Nov 01 '22 13:11

Aldo 'xoen' Giambelluca