go - 在库中使用外部依赖

标签 go

我正在使用 wgo对于Golang中的依赖管理(虽然我觉得wgo跟这个关系不大),wgo有这样一个文件夹结构

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

我有一个我自己编写的库,它在其中一个导出方法中使用 nsq-go 类型:

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

这个库叫做messi,我像这样导入nsq-go "messi/vendor/src/github.com/bitly/go-nsq "

当我尝试在另一个项目中使用这个库时,问题就来了。例如,在名为 scribe 的项目中,我有以下代码(注意导入):

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())

当我go build 时返回以下错误:

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

为什么?我真的不知道发生了什么。导入的代码go-nsq一模一样,golang却要这段代码来自同一个文件夹?

我做错了什么?

最佳答案

Go 中的包由完整导入路径而非名称标识。

例如在标准库中有两个不同的包,它们具有相同的名称 template 但导入路径不同:text/templatehtml/template.

您应该确保使用相同的路径导入 go-nsq 包。

关于go - 在库中使用外部依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969088/

相关文章:

unit-testing - 去单元测试找不到所需的文件

unit-testing - 如何对从包中导入的方法进行 stub 和 spy 事件?

go - 我的更改并没有在 go 应用程序中结束。 go 是怎么回事?

ssl - 使用 golang mutual TLS auth 信任特定客户端

html - 为什么这个程序不打印任何东西?

go - 奇怪的零/非零行为

google-app-engine - 如何使用 Go 中的 Google Spreadsheet API?

mysql - 与 SQL 的时区协调

go - 如何提取表单数据字段名称

go - 在 Golang 应用程序中使用 Couchbase Lite(不是 Couchbase Server)