On Thu, May 25, 2023 at 5:35 AM 'Pranay Agarwal' via golang-nuts <golang-nuts@googlegroups.com> wrote: > > I am using Go 1.20.4 on Ubuntu 22.04. I am facing a compilation error when I > attempt to pass a map from a string to a struct to a function that expects a > map from string to an interface. > > Here is some example code demonstrating the issue: > https://goplay.tools/snippet/GrPfTWfycdx > > Here, the struct is Helper, and it conforms to both interface definitions - > Helper1 and Helper2. I am able to pass in an instance of Helper directly, > however when I attempt to pass a map[string]Helper the compiler throws a type > error IncompatibleAssign. > > I was wondering if anyone else has faced something similar? And if so, how > did you solve this issue. Would appreciate any input, thank you.
Go doesn't work that way. You can't freely interchange a type with an interface type, even if the type implements the interface. For a related scenario, see https://go.dev/doc/faq#covariant_types. The only fix is to be consistent: if you need a map to an interface, build a map to an interface type. If you need to support two different interfaces, build a map to any, and use a type assertion. Ian -- 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/CAOyqgcUqt0MCBHhswfJkkbRfj3dRnojWtbaVqHF%3Da_M248EF1A%40mail.gmail.com.