dstbtam8732
2019-06-05 20:27
浏览 36
已采纳

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

图片转代码服务由CSDN问答提供 功能建议

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

 类型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(  
设置)
返回测试{} 
} 
   
 
 

但是,出现以下错误

 < 代码>连线:D:\ git \ go \ vendor.manager \ src \ lib \ di \ appbuild.go:50:5:** vendor.manager / lib / di.MyFooer不实现vendor.manager / lib / di  .Fooer 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyi1015 2019-06-05 20:32
    已采纳

    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))
    
    点赞 评论

相关推荐 更多相似问题