News for hackers. Stuff that matters.
Re:Go Is a Well-Designed Language by fullstackchris on Friday January 31, 2025 @03:00AM (#42885543)
package main

import "fmt"

// Define an enumeration for status type Status int

const ( Pending Status = iota InProgress Completed Failed )

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

func main() { var s Status = InProgress fmt.Println(s) // Output: InProgress }

Re:Go Is a Well-Designed Language by physicsguy on Friday January 31, 2025 @03:22AM (#42885659)
I see this suggested all the time as though it’s supposed to be a replacement. It isn’t sufficient, it’s a poor emulation of what enums can be used for and doesn’t offer the same protections, since Status can be set to any integer value without casting or any knowledge that it’s broken.

In your suggested method it doesn’t even catch that case and provide an error if the value isn’t in the range.

Re:Go Is a Well-Designed Language by powerhugs on Friday January 31, 2025 @03:41AM (#42885778)
It seems like none of the other comments here use or are even aware of `go generate`. Go has excellent code generation features [1].

I think parent should be using `stringer` [2] instead.

1: https://go.dev/blog/generate

2: https://pkg.go.dev/golang.org/x/tools/cmd/stringer

Re:Go Is a Well-Designed Language by physicsguy on Friday January 31, 2025 @03:44AM (#42885799)
I'm well aware of Stringer but I don't again think it's a good replacement of having enums built in!
Re:Go Is a Well-Designed Language by powerhugs on Friday January 31, 2025 @04:31AM (#42886093)
> I don't again think it's a good replacement of having enums built in!

I didn't say anything along those lines, in fact I was commenting generally about this issue that you guys and the other commenters mentioned.

I was just suggesting a solution to the the problem stated.

Re:Go Is a Well-Designed Language by fullstackchris on Friday January 31, 2025 @01:34PM (#42890278)
"We don't want a solution! We want to complain about what we are used to and dont have!"
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-

Re:Go Is a Well-Designed Language by Svip on Friday January 31, 2025 @03:04AM (#42885568)
OK, but I can set s to 1000, and it still compiles.
Re:Go Is a Well-Designed Language by pjmlp on Friday January 31, 2025 @03:24AM (#42885685)
Also known as design hack, workaround for what other languages support natively since 1970's.

Only one step better than Assembly or Fortran.