Comment by dfawcus

Comment by dfawcus 9 days ago

1 reply

In a specific scenario, I have made use of interface values, and type switches as a form of "tagged union" / "sum type".

All it requires is that the module defining the interface, and structs implementing it, are in the same package, and the interface requires a private method.

I used that to have a message type, passing instances of it over a channel of that interface, and demuxing based on a type switch of the message.

One could use a similar scheme for return values from functions, just that for the simple error / no-error case it would not be idiomatic, however that should not prevent one from doing so if desired.

ngrilly 9 days ago

Yes, that’s a possible pattern to emulate a sum type in Go.