What is the best way to use interface for interaction between packages?

hi, i’m looking for a solution on how to bind two packages.
i did an example (which does not work, unfortunately)
https://play.golang.org/p/j6lMFn9dXnc

this produces an error:
# play.ground
./prog.go:11:15: cannot use dlg (type dialog.Dialog) as type config.dialog in argument to cfg.SetDialog:
dialog.Dialog does not implement config.dialog (wrong type for SetGlobalConfig method)
have SetGlobalConfig(dialog.Config)
want SetGlobalConfig(config.Config)

main related links that i learned:
https://golang.org/ref/spec#Type_identity
and

as far as i understand the reason is:

A defined type is always different from any other type.

i look at the example from here https://github.com/golang/go/wiki/CodeReviewComments#interfaces and see no difference with what i do. i tried to simplify the logic by removing parameter in the SetGlobalConifg function and then it works.
https://play.golang.org/p/JImfa3POk6e
But in this case, i cannot send config instance into dialog and vice versa.

So the question is - is there a solution how to send object between the package?

I’m on my phone and can’t take an in-depth look right now, but I think you have to use an interface type as your Config type; it can’t be a struct.

I answered a related question a few hours ago here. See the part about interface types and the myReader type I used in my example.

hi Sean, thank you for the extensive explanation in related post! What i noticed, that mostly core types operate with standard types, like bool, int, string, etc. And what if we want to use some custom external type? we do want to copy/implement in the same package. I assume interface would help here. This is what I’m trying to implement actually in my code.

I think you have to use an interface type as your Config type; it can’t be a struct.

could you give a short example please of how you would do that?

I tried to work with the example you gave, but I’m having a hard time understanding what is configuring what, so I put together a different example here: https://play.golang.org/p/dq9eB2DXArI

@skillian thank you very much for the example, this is very valuable!
your example has simple types in the interface. I tried to work with the type which contains property where the type is from a different package. and tried to did it using interfaces. seems, it looks wrong.

go has only one access vector from root path to package. My idea was to use root package as a communicator between two packages, but each package knows about another one only theoretically, so the logic is kept in the related package then. If it’s wrong, it means only root package has to operate with the children packages but packages between each other cannot even using interfaces.

is there best practice to create correct apps architecture in go? This is not my the first attempt to build some small project in go for better understanding and all these times interface understanding is blocking me. I came from PHP world, i understand the difference between interfaces in classic OOP and in go, but i cannot find the strict approach in apps architecture design. May you have some tips?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.