Enums without prefixes?

Hi, does anyone know how I could write

const (cc_brown cow_color_t = iota, cc_white)
const (pc_pink parrot_color_t = iota, pc_white)


const (brown cow_color_t = iota, white)
const (pink parrot_color_t = iota, white)

Some prefixes are hard to remember, but longer prefixes can be laborious to write. Is there a way to use the type as lexical scope and leave it out in switch statements and assignments?
Thank you

I can’t quite tell what you’re asking: In your first code block, you’re defining enums with prefixes where you have two separate values: cc_white and pc_white which have different types. In your second example, you’re redefining white and I’m not sure what you expect; when you use white in your code later, should it be of type cow_color_t or parrot_color_t?

Thanks for asking. The type should be inferred. The point is just to avoid typing the prefixes again and again. In switch cases it’s inferred from the top line that says “switch …” and in assignments from the type of the left hand side. There’s a few languages that do that with their enums, not sure which ones, I forgot about that.

PS: It would need some additional operator. Like “.white”, “::white” something like that. I just wanted to ask if there’s some trick in Go to do the same, without Go really supporting it.

You can leave white as an untyped constant:

type cow_color_t int
type parrot_color_t int

const white = 1

const (
	brown cow_color_t = iota
const (
	pink parrot_color_t = iota

Hm, yes, I would need to sort my enums for this approach, and the constants can clash with other names, too, so there would be some organizing involved.
I’m content to know I don’t miss any Go feature, which was the purpose for my question.
Thank you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.