duandao1931 2015-08-12 15:09
浏览 298
已采纳

在库中使用外部依赖关系

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?

  • 写回答

1条回答 默认 最新

  • doujingxi3356 2015-08-12 15:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退