const
Color
type Color string

const (
    Red   Color = "red"
    Blue  Color = "blue"
    Green Color = "green"
)
Color
func main() {
    var c Color = Blue
    fmt.Println(c)
}
blue

注意,枚举类型在某些情况下可以提供类型安全和更清晰的代码,但在其他情况下可能会增加不必要的复杂性。因此,在选择使用枚举时,请考虑您的具体需求和实现方式。