[go-nuts] Map of Types is impossible, is there any alternative solution?

2023-04-11 Thread Bèrto ëd Sèra
I do know there's no way to use var rule = map[uint8]Message{ 0: thisType, 1: thatType, 4: someOtherType, . } type Message interface { Become(data []byte) } type thisType struct { data []byte } func (my *thisType) Become(data []byte) { } type thatType struct { da

Re: [go-nuts] Map of Types is impossible, is there any alternative solution?

2023-04-11 Thread burak serdar
You can do: var messageProcessors = map[uint8]func([]byte) Message { 0: processorForType0, 1: processorForType1, ... } Then: output:=messageProcessors[id](payload) Of course, with the appropriate error checks. On Tue, Apr 11, 2023 at 3:13 PM Bèrto ëd Sèra wrote: > I do know there's no

Re: [go-nuts] Map of Types is impossible, is there any alternative solution?

2023-04-11 Thread Rob Pike
I had written out a message to suggest this approach but deleted it because it still required a type switch afterwards. Later I realized that the variant types of the message could easily be handled by an interface, like this: type Message interface { Build([]byte) Message Handle() error } T