News for hackers. Stuff that matters.
Re:Writing secure Go code by dfawcus on Tuesday November 05, 2024 @12:41PM (#42053583)
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.

Re:Writing secure Go code by ngrilly on Tuesday November 05, 2024 @01:01PM (#42053741)
Yes, that’s a possible pattern to emulate a sum type in Go.