eversor
eversor

Reputation: 3073

Using an External Dependency in a Library

I am using wgo for dependency management in Golang (although I think wgo has little to do with this), wgo has a folder structure like this

project/
    .gocfg/
        gopaths
        vendor.json
    vendor/
        src/
            github.com_or_whatever/

I have a library I coded myself which uses an nsq-go type in one of the exported methods:

func AddNsqSubscription(
    topic, channel string, 
    handler nsq.Handler, 
    config *nsq.Config) error { }

The library is called messi and I import the nsq-go like so "messi/vendor/src/github.com/bitly/go-nsq"

The problem comes when I try to use this library in another project. For instance, in a project called scribe I have the following code (notice the imports):

import (
    "scribe/vendor/src/github.com/bitly/go-nsq"
    "scribe/vendor/src/messi"
)

//...
nsqHandler := nsq.HandlerFunc(func(message *nsq.Message) error {
    msgHandler(MessiMessage{message})
    return nil
})

return messi.AddNsqSubscription(destination, subdestination, nsqHandler, nsq.NewConfig())

When I go build the following error is returned:

cannot use nsqHandler (type "scribe/vendor/src/github.com/bitly/go-nsq".HandlerFunc) as type "messi/vendor/src/github.com/bitly/go-nsq".Handler in argument to messi.AddNsqSubscription: "scribe/vendor/src/github.com/bitly/go-nsq".HandlerFunc does not implement "messi/vendor/src/github.com/bitly/go-nsq".Handler (wrong type for HandleMessage method)

have HandleMessage("scribe/vendor/src/github.com/bitly/go-nsq".Message) error
want HandleMessage(
"messi/vendor/src/github.com/bitly/go-nsq".Message) error

Why? I do not really know what is going on. The code go-nsq imported is exactly the same, yet golang wants that this code comes from the same folder?

What am I doing wrong?

Upvotes: 0

Views: 1701

Answers (1)

kostya
kostya

Reputation: 9559

Packages in Go are identified by full import path, not by name.

For example in the standard library there are two different packages with the same name template but different import paths: text/template and html/template.

You should make sure that go-nsq package is imported using the same path.

Upvotes: 2

Related Questions