Go中的可打印枚举

In some cases, it's convenient for user interaction and debugging purposes to have a human-readable string representation of enums. So far, the best I have come up with is:

type ElementType int

const (
    Fire = ElementType(iota)
    Air
    Water
    Earth
)

var elementTypeMap = map[ElementType]string{
    Fire: "The Fiery Fire",
    Air: "The Airy Air",
    Water: "The Watery Water",
    Earth: "The Earthy Earth",
}

func (el ElementType) String() string {
    return elementTypeMap[el]
}

The above, allows me to use and pass the enum as an int, keeping its standard performance, and to easily print its string representation anywhere. The only drawback is that there is an amount of boilerplate code that adds up if you have many enum types: I would be rather happy to avoid it.

Is there a way, preferably an idiomatic one, to reduce the boilerplate code above?

This looks dryer (and faster) :

type ElementType int

const (
    Fire = ElementType(iota)
    Air
    Water
    Earth
)

var elementnames = [...]string {
    "The Fiery Fire",
    "The Airy Air",
    "The Watery Water",
    "The Earthy Earth"
}

func (el ElementType) String() string {
    return elementnames[el]
}

Note that there was a discussion on golang-nuts on whether giving a generic solution to assign names to enum constants and as far as I know it wasn't seen as necessary (see https://groups.google.com/forum/#!topic/golang-nuts/fCdBSRNNUY8).

There are alternative ways. For example, using a type elementType struct { name string } and var Fire = &elementType{"The Fiery Fire"} comes to my mind. You will still just pass and compare pointers, but you can attach arbitrary data to it.

I do not exactly know your use-case, but I would probably do it in a similar way:

type ElementType int

const (
    Fire = ElementType(iota)
    Air
    Water
    Earth
)

func (e ElementType) String() string {
    var names = [...]string{
        Fire:  "The Fiery Fire",
        Air:   "The Airy Air",
        Water: "The Watery Water",
        Earth: "The Earthy Earth",
    }
    return names[e]
}

A map is computationally quite expensive. You can use an array and still list the elements in an arbitrary way. Also, making the array a local variable might make later changes easier. And you should probably also add an if e < 0 || e >= len(names) there, especially if your ElementType is exported.

I don't think arrays and maps are the way to go here. You have to manually define the elements anyway, so I think switch is the clearest. Like a map, and unlike an array, it lets you use non-dense constants (meaning constants other than 0,1,2,3...). It also allows you to define a case in case something goes terribly wrong and you use a non-constant; at the very least you can define a custom panic message.

func (et ElementType) String() string {
    switch et {
    case Water:
        return "The Watery Water"
    case Air:
        return "The Airy Air"
    case Fire:
        return "The Fiery Fire"
    case Earth:
         return "The Earthy Earth"
    default:
         return "Heart has been banned from the elements"
    }
}

I'd also wager that the compiler can do some fancy optimizations with this that it can't do with array/map access since those are declared using var rather than const.