yesIcan
yesIcan

Reputation: 1701

Golang wire is throwing wire: type x does not implement interface error

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

Upvotes: 1

Views: 2066

Answers (1)

Adrian
Adrian

Reputation: 46413

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))

Upvotes: 2

Related Questions