Create an enum from a group of related constants in Go

19,258

Solution 1

const?

const (
    pi = 3.14
    foo = 42
    bar = "hello"
)

Solution 2

There are two options, depending on how the constants will be used.

The first is to create a new type based on int, and declare your constants using this new type, e.g.:

type MyFlag int

const (
    Foo MyFlag = 1
    Bar
)

Foo and Bar will have type MyFlag. If you want to extract the int value back from a MyFlag, you need a type coersion:

var i int = int( Bar )

If this is inconvenient, use newacct's suggestion of a bare const block:

const (
    Foo = 1
    Bar = 2
)

Foo and Bar are perfect constants that can be assigned to int, float, etc.

This is covered in Effective Go in the Constants section. See also the discussion of the iota keyword for auto-assignment of values like C/C++.

Solution 3

My closest approach to enums is to declare constants as a type. At least you have some type-safety which is the major perk of an enum type.

type PoiType string

const (
    Camping            PoiType = "Camping"
    Eatery             PoiType = "Eatery"
    Viewpoint          PoiType = "Viewpoint"
)

Solution 4

It depends on what do you want to achieve by this grouping. Go allows grouping with the following braces syntax:

const (
    c0 = 123
    c1 = 67.23
    c2 = "string"
)

Which just adds nice visual block for a programmer (editors allow to fold it), but does nothing for a compiler (you can not specify a name for a block).

The only thing that depends on this block is the iota constant declaration in Go (which is pretty handy for enums). It allows you to create auto increments easily (way more than just auto increments: more on this in the link).

For example this:

const (
    c0 = 3 + 5 * iota
    c1
    c2
)

will create constants c0 = 3 (3 + 5 * 0), c1 = 8 (3 + 5 * 1) and c2 = 13 (3 + 5 * 2).

Share:
19,258
Darius Kucinskas
Author by

Darius Kucinskas

I'm a software developer, a blogger, a husband and a father. Over the last few years I have been writing web apps in C#\JavaScript. Previously I was a C++\Python programmer.

Updated on June 03, 2022

Comments

  • Darius Kucinskas
    Darius Kucinskas about 2 years

    What is preferred (or right) way to group large number of related constants in the Go language? For example C# and C++ both have enum for this.

  • Darius Kucinskas
    Darius Kucinskas almost 13 years
    could such block have a name?
  • Zippo
    Zippo almost 13 years
    @Darius: no :-( anyway this is not grouping.
  • Edwin Biemond
    Edwin Biemond almost 13 years
    This isn't quite correct. In your first example Bar is untyped. It would only pick up Foo's type if the right side of the assignment were blank. You'd be right to use iota in this case.
  • Edwin Biemond
    Edwin Biemond almost 13 years
    You can give a block a name in a sense if you give all the constants the same user-defined type. Sort of like what lnmx is trying to do in the MyFlag example.
  • lnmx
    lnmx almost 13 years
    @Evan corrected, thank you. I tested my example before posting the answer, but somehow the extra assignment popped in there.
  • Edwin Biemond
    Edwin Biemond almost 13 years
    I'm not sure if you intended it or not, but now they have the same value and type.
  • chakrit
    chakrit about 10 years
    "It would only pick up Foo's type if the right side of the assignment were blank." — Just got bitten by that gotcha. Emphasis here so other people notes it.