Client.GetChild() must return GetChilder, not Child. On Wed, Mar 13, 2024 at 10:02 AM Rodrigo Araujo <yol...@gmail.com> wrote:
> Given the code bellow, I'm trying to keep my packages completely > separated, without knowing each other, but the code doesn't work. I just > can get it running when I define (or use) an interface from one package > inside code of the other package. > Here is the error: > > > > *cannot use client (variable of type *a.Client) as b.GetChilder value in > struct literal: *a.Client does not implement b.GetChilder (wrong type for > method GetChild) have GetChild() *a.Child want GetChild() > b.GetDataercompilerInvalidIfaceAssign > <https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#InvalidIfaceAssign>* > > package a // file: a/a.go > > import "fmt" > > type Client struct{} > > func (c *Client) GetChild() *Child { > return &Child{} > } > > type Child struct{} > > func (c *Child) GetData() { > fmt.Println("from GetData") > } > > > package b // file: b/b.go > > type GetDataer interface { > GetData() > } > > type GetChilder interface { > GetChild() GetDataer > } > > type Processor struct { > Client GetChilder > } > > func (p *Processor) Process() { > card := p.Client.GetChild() > card.GetData() > } > > > package main // file: main.go > > import ( > "interfaces/a" > "interfaces/b" > ) > > func main() { > client := &a.Client{} > processor := &b.Processor{ > Client: client, > } > processor.Process() > } > > > Why this doens't work? Some kind of limitation on interfaces usage? > How is the best approach (idiomatic golang) to tacke this kind of problem? > > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/41be202c-f205-4adf-aecc-0f8e681e2490n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/41be202c-f205-4adf-aecc-0f8e681e2490n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CANGiwgaPDFoTydTy7WeqECm%2B4yyH7h8qrCkY6m70sh2Gxk553Q%40mail.gmail.com.