News for hackers. Stuff that matters.
Re:Go Is a Well-Designed Language by alkonaut on Friday January 31, 2025 @03:10AM (#42885603)
Next day I have this (added one entry). I forgot to update the string list elsewhere however.

const ( Pending Status = iota InProgress Completed Cancelled Failed )

func (s Status) String() string { return [...]string{"Pending", "InProgress", "Completed", "Failed"}[s] }

func main() { var s Status = Cancelled fmt.Println(s) // Output: ??

If the compiler - not some linter - protests that the list of names is different from the number of items in the enum, then I think this is at least a half-decent design of an enum type. Not a great one (because the author still had to repeat the names), but at least something that isn't a fundamentally broken design of an enum type.

But if the compiler is silent, and the output of the Println(Cancelled) is "Failed" then I'm not angry, I'm disappointed.

Edit: https://go.dev/play/p/MMPMh7_U81-

There are no comments on this comment yet.