【发布时间】:2019-05-22 14:25:30
【问题描述】:
在 golang 中是否有简单的枚举实现? 类似以下内容?
type status enum[string] {
pending = "PENDING"
active = "ACTIVE"
}
【问题讨论】:
标签: go
在 golang 中是否有简单的枚举实现? 类似以下内容?
type status enum[string] {
pending = "PENDING"
active = "ACTIVE"
}
【问题讨论】:
标签: go
const (
statusPending = "PENDING"
statusActive = "ACTIVE"
)
或者,在Ultimate Visual Guide to Go Enums 应用示例
// Declare a new type named status which will unify our enum values
// It has an underlying type of unsigned integer (uint).
type status int
// Declare typed constants each with type of status
const (
pending status = iota
active
)
// String returns the string value of the status
func (s status) String() string {
strings := [...]string{"PENDING", "ACTIVE"}
// prevent panicking in case of status is out-of-range
if s < pending || s > active {
return "Unknown"
}
return strings[s]
}
【讨论】:
【讨论】: