golang 枚举示例

 1package main
 2
 3import  (
 4  "fmt"
 5)
 6
 7type PolicyType int32
 8
 9const (
10    Policy_MIN      PolicyType = 0
11    Policy_MAX      PolicyType = 1
12    Policy_MID      PolicyType = 2
13    Policy_AVG      PolicyType = 3
14)
15
16func (p PolicyType) String() string {
17    switch (p) {
18    case Policy_MIN: return "MIN"
19    case Policy_MAX: return "MAX"
20    case Policy_MID: return "MID"
21    case Policy_AVG: return "AVG"
22    default:         return "UNKNOWN"
23    }
24}
25
26func foo(p PolicyType) {
27    fmt.Printf("enum value: %v\n", p)
28}
29
30func main() {
31    foo(Policy_MAX)
32}

发布日期:2023-04-16 22:30 字数:92 用时 1分钟
tags:golang