我正在使用 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/template
和 html/template
.
您应该确保使用相同的路径导入 go-nsq
包。
关于go - 在库中使用外部依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969088/