在库中使用外部依赖项

huangapple go评论84阅读模式
英文:

Using an External Dependency in a Library

问题

我正在使用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时,会返回以下错误:

> 无法使用 nsqHandler(类型为"scribe/vendor/src/github.com/bitly/go-nsq".HandlerFunc)作为messi.AddNsqSubscription的参数类型"messi/vendor/src/github.com/bitly/go-nsq".Handler:
"scribe/vendor/src/github.com/bitly/go-nsq".HandlerFunc未实现"messi/vendor/src/github.com/bitly/go-nsq".Handler(HandleMessage方法的类型不正确)

> HandleMessage("scribe/vendor/src/github.com/bitly/go-nsq".Message) error
想要 HandleMessage(
"messi/vendor/src/github.com/bitly/go-nsq".Message) error

为什么? 我真的不知道发生了什么。导入的go-nsq代码完全相同,但是Golang希望这段代码来自同一个文件夹?

我做错了什么?

英文:

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

得分: 2

在Go语言中,包是通过完整的导入路径来标识的,而不是通过名称。

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

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

英文:

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.

huangapple
  • 本文由 发表于 2015年8月12日 23:09:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/31969088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定