确实,上面使用const 和iota 的示例是Go 中表示原始枚举的最惯用的方式。但是,如果您正在寻找一种方法来创建功能更全面的枚举,类似于您在其他语言(如 Java 或 Python)中看到的类型呢?
在 Python 中创建一个看起来和感觉上都像字符串枚举的对象的一种非常简单的方法是:
package main
import (
"fmt"
)
var Colors = newColorRegistry()
func newColorRegistry() *colorRegistry {
return &colorRegistry{
Red: "red",
Green: "green",
Blue: "blue",
}
}
type colorRegistry struct {
Red string
Green string
Blue string
}
func main() {
fmt.Println(Colors.Red)
}
假设您还需要一些实用方法,例如Colors.List() 和Colors.Parse("red")。而且你的颜色更复杂,需要成为一个结构。然后你可能会做一些这样的事情:
package main
import (
"errors"
"fmt"
)
var Colors = newColorRegistry()
type Color struct {
StringRepresentation string
Hex string
}
func (c *Color) String() string {
return c.StringRepresentation
}
func newColorRegistry() *colorRegistry {
red := &Color{"red", "F00"}
green := &Color{"green", "0F0"}
blue := &Color{"blue", "00F"}
return &colorRegistry{
Red: red,
Green: green,
Blue: blue,
colors: []*Color{red, green, blue},
}
}
type colorRegistry struct {
Red *Color
Green *Color
Blue *Color
colors []*Color
}
func (c *colorRegistry) List() []*Color {
return c.colors
}
func (c *colorRegistry) Parse(s string) (*Color, error) {
for _, color := range c.List() {
if color.String() == s {
return color, nil
}
}
return nil, errors.New("couldn't find it")
}
func main() {
fmt.Printf("%s\n", Colors.List())
}
到那时,确定它可以工作,但您可能不喜欢重复定义颜色的方式。如果此时你想消除它,你可以在你的结构上使用标签并做一些花哨的反射来设置它,但希望这足以覆盖大多数人。