Golang电线掉线:x类型未实现接口错误

问题描述:

Here is a sample code most of it is copied from official golang doc here I only added the last piece of code which generates an instance for a type that consumes Fooer interface.

 type Fooer interface {
        Foo() string
}

type MyFooer string

func (b *MyFooer) Foo() string {
    return string(*b)
}

func provideMyFooer() *MyFooer {
    b := new(MyFooer)
    *b = "Hello, World!"
    return b
}

type Bar string

func provideBar(f Fooer) string {
    // f will be a *MyFooer.
    return f.Foo()
}

type test struct {
    f Fooer 
}
var Set = wire.NewSet(
    provideMyFooer,
    wire.Bind(new(Fooer), new(*MyFooer)),
    provideBar)
// InitializeMasterRepo init repo
func testbuild() test  {
    wire.Build(
        Set)
    return test{}
}

However, I am getting the following error

wire: D:\git\go\vendor.manager\src\lib\di\appbuild.go:50:5: **vendor.manager/lib/di.MyFooer does not implement vendor.manager/lib/di.Fooer

这里是示例代码,大部分都是从官方golang文档复制而来的此处 我只添加了最后一部分代码,该代码生成了一种类型的实例 使用Fooer接口。 p>

 类型Fooer接口{
 Foo()字符串
} 
 
键入MyFooer字符串
 
func(b * MyFooer)Foo()字符串{
返回 string(* b)
} 
 
func ProvideMyFooer()* MyFooer {
b:= new(MyFooer)
 * b =“ Hello,World!” 
返回b 
} 
 
type条形字符串 
 
func ProvideBar(f Fooer)字符串{
 // f将是* MyFooer。
返回f.Foo()
} 
 
type测试结构{
f Fooer 
} 
var Set =  wire.NewSet(
 ProvideMyFooer,
 wire.Bind(new(Fooer),new(* MyFooer)),
provideBar)
 // InitializeMasterRepo init repo 
func testbuild()test {
 wire.Build(  
设置)
返回测试{} 
} 
  code>  pre> 
 
 

但是,出现以下错误 p>

 连线:D:\ git \ go \ vendor.manager \ src \ lib \ di \ appbuild.go:50:5:** vendor.manager / lib / di.MyFooer不实现vendor.manager / lib / di  .Fooer 
  code>  pre> 
  div>

The type is wrong. Your receiver is *MyFooer; your value is (as the error says) **MyFooer. This is because you're calling new(*MyFooer); new already returns a pointer to the type passed, so since you're passing a pointer type to it, you're getting a pointer to a pointer.

Change the line as follows to fix this error:

wire.Bind(new(Fooer), new(MyFooer))