在不同的包中定义的接口无法正常工作。

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

interface defined in one package doesn't work in a different package

问题

我有一个在goQA包中定义的简单接口,并使用实现该接口的结构体:

type ReportWriter interface {
    Name() string
    Init(parent ITestManager)
    onManagerStatistics(report *ManagerResult, stats *ReporterStatistics, name, msg string)
}

type MongoReporter struct {
}

func (t *MongoReporter) Name() string {
}

func (t *MongoReporter) Init(parent ITestManager) {
}

func (t *MongoReporter) onManagerStatistics(report *ManagerResult, stats *ReporterStatistics, name, msg string) {
}

然后我可以在示例文件中创建一个变量,一切都正常工作:

var mr goQA.ReportWriter
mr = &goQA.MongoReporter{}

问题出现在将结构体移动到自己的包mongo并导入goQA包时。除了使用包名之外,一切都相同:

type MongoReporter struct {
}

func (t *MongoReporter) Name() string {
}

func (t *MongoReporter) Init(parent goQA.ITestManager) {
}

func (t *MongoReporter) onManagerStatistics(report *goQA.ManagerResult, stats *goQA.ReporterStatistics, name, msg string) {
}

我尝试像之前一样在示例程序中使用结构体:

var mr goQA.ReportWriter
mr = &mongo.MongoReporter{}

出现了错误消息:

examples\example_mongo1.go:108: cannot use mongo.MongoReporter literal (type *mongo.MongoReporter) as type goQA.ReportWriter in assignment:
        *mongo.MongoReporter does not implement goQA.ReportWriter (missing goQA.onManagerStatistics method)
                have mongo.onManagerStatistics(*goQA.ManagerResult, *goQA.ReporterStatistics, string, string)
                want goQA.onManagerStatistics(*goQA.ManagerResult, *goQA.ReporterStatistics, string, string)

为什么它说"have mongo.onManagerStatistics but want goQA.onManagerStatistics"?签名不同吗?
为什么没有抱怨Init()和Name()方法?

将Name()方法更改为Name(i int) string后,错误是:

have Name(int) string
want Name() string

它没有说:

have mongo.Name(int) string
want goQA.Name() string

我不明白这里的错误是什么。看起来不像是简单的接口实现错误。

英文:

I have a simple interface defined in a package goQA and use it with a struct that implements the interface:

type ReportWriter interface {
    Name() string
    Init(parent ITestManager)
    onManagerStatistics(report *ManagerResult, stats *ReporterStatistics, name, msg string)
}


type MongoReporter struct {
}

func (t *MongoReporter) Name() string {
}

func (t *MongoReporter) Init(parent ITestManager) {
}

func (t *MongoReporter) onManagerStatistics(report *ManagerResult, stats *ReporterStatistics, name, msg string) {
}

I can then create a variable in an example file and everything works fine:

var mr goQA.ReportWriter
mr = &goQA.MongoReporter{}

The problem came when moving the struct to it's own package, mongo, and importing the goQA package. Everything is the same except using the package name:

type MongoReporter struct {
}

func (t *MongoReporter) Name() string {
}

func (t *MongoReporter) Init(parent goQA.ITestManager) {
}

func (t *MongoReporter) onManagerStatistics(report *goQA.ManagerResult, stats *goQA.ReporterStatistics, name, msg string) {
}

I Try and use struct in example program like before:

var mr goQA.ReportWriter
mr = &mongo.MongoReporter{}

There is an error message:

""""examples\example_mongo1.go:108: cannot use mongo.MongoReporter literal (type *mongo.MongoReporter) as type oQA.ReportWriter in assignment:
*mongo.MongoReporter does not implement goQA.ReportWriter (missing goQA.onManagerStatistics method) have mongo.onManagerStatistics(*goQA.ManagerResult, *goQA.ReporterStatistics, string, string)
want goQA.onManagerStatistics(*goQA.ManagerResult, *goQA.ReporterStatistics, string, string)""""

Why does it say "have mongo.onManagerStatistics but want goQA.onManagerStatistics?" Is the signature different?
Why not complain about Init() and Name() methods?

After changing the Name() string method to Name(i int) string the error is

have Name(int) string
want Name() string

Didn't say:

have mongo.Name(int) string
want goQA.Name() string

I don't understand what the error is here. Doesn't look like the a simple error in implementing the interface.

答案1

得分: 5

包B如何提供一个满足包A接口的类型,而包A中有未导出的方法?确切地说:它是无法做到的。你需要将onManagerStatistics导出,即首字母大写。

英文:

How could package B provide a type which fulfills an interface of package A which has unexported methods? Exactly: It can't. You will have to export onManagerStatistics with a capital O.

huangapple
  • 本文由 发表于 2014年4月25日 02:52:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/23277395.html
匿名

发表评论

匿名网友

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

确定