go语言实现枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
type Season int
 
const (
    Summer Season = iota + 1
    Autumn
    Winter
    Spring
)
 
type ArticleState int
 
const (
  Draft ArticleState = iota + 1
  Published
  Deleted 
)
 
func checkArticleState(state ArticleState) bool {
    // ...
}
 
 func main() {
   // 两个操作数类型不匹配,编译错误
   fmt.Println(Autumn == Draft)
    // 参数类型不匹配,但是因为 ArticleState 底层的类型是 int 所以传递 int 的时候会发生隐式类型转换,所以不会报错
   checkArticleState(100)
 }

  

 

参考:Go 里面该怎么实现枚举 · Issue #73 · kevinyan815/gocookbook · GitHub

posted @   ☞@_@  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示