在 Go语言中实现枚举
发布于 2021-11-04 14:19 ,所属分类:软件编程学习资料
关注微信公众号《云原生CTO》更多云原生干货等你来探索
专注于云原生技术分享
提供优质云原生开发视频技术培训
面试技巧,及技术疑难问题解答

云原生技术分享不仅仅局限于Go、Rust、Python、Istio、containerd、CoreDNS、Envoy、etcd、Fluentd、Harbor、Helm、Jaeger、Kubernetes、OpenPolicyAgent、Prometheus、Rook、TiKV、TUF、Vitess、Argo、Buildpacks、CloudEvents、CNI、Contour、Cortex、CRI-O、Falco、Flux、gRPC、KubeEdge、Linkerd、NATS、Notary、OpenTracing、OperatorFramework、SPIFFE、SPIRE和Thanos等


在 Go中实现枚举
枚举可以将相关的常量归为一种类型。枚举是一个强大的功能,用途广泛。然而,在 Go 中,它们的实现方式与大多数其他编程语言大不相同。

注意:在你继续之前,我希望你们都对 Go 语法和原始类型有一个初级的理解,以理解下面的代码。
在本文中,我将向您展示如何使用预先声明的标识在 Golang 中实现枚举iota,以及它是如何使用的。
什么是iota?
iota是一个标识符,constant它可以简化使用自动递增数字的常量定义。iota关键字代表整数从零开始.
iota 关键字表示连续的整数常量 0, 1, 2,...。只要在源代码中出现 const 一词,它就会重置为 0,并在每个 const 规范后递增。
packagemain
import"fmt"
const(
c0=iota
c1=iota
c2=iota
)
funcmain(){
fmt.Println(c0,c1,c2)//Print:012
}
您可以避免iota在每个常量前面写连续的= iota。这可以简化为以下代码清单:
packagemain
import"fmt"
const(
c0=iota
c1
c2
)
funcmain(){
fmt.Println(c0,c1,c2)//Print:012
}
要将常量列表从 1 而不是 0开始,您可以iota在算术表达式中使用。
packagemain
import"fmt"
const(
c0=iota+1
c1
c2
)
funcmain(){
fmt.Println(c0,c1,c2)//Print:123
}
您可以使用空白标识符跳过常量列表中的值。
packagemain
import"fmt"
const(
c1=iota+1
_
c3
c4
)
funcmain(){
fmt.Println(c1,c3,c4)//Print:134
}
使用 iota 实现枚举
为了实现我们必须执行的自定义枚举类型考虑以下
声明一个新的自定义类型——整数类型。 声明相关常量——使用iota. 创建通用行为——给类型一个String函数。 创建额外的行为——给类型一个EnumIndex函数。
示例 1:为工作日创建一个枚举。

packagemain
import"fmt"
//Weekday-Customtypetoholdvalueforweekdayrangingfrom1-7
typeWeekdayint
//Declarerelatedconstantsforeachweekdaystartingwithindex1
const(
SundayWeekday=iota+1//EnumIndex=1
Monday//EnumIndex=2
Tuesday//EnumIndex=3
Wednesday//EnumIndex=4
Thursday//EnumIndex=5
Friday//EnumIndex=6
Saturday//EnumIndex=7
)
//String-Creatingcommonbehavior-givethetypeaStringfunction
func(wWeekday)String()string{
return[...]string{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}[w-1]
}
//EnumIndex-Creatingcommonbehavior-givethetypeaEnumIndexfunction
func(wWeekday)EnumIndex()int{
returnint(w)
}
funcmain(){
varweekday=Sunday
fmt.Println(weekday)//Print:Sunday
fmt.Println(weekday.String())//Print:Sunday
fmt.Println(weekday.EnumIndex())//Print:1
}
示例 2:为方向创建一个枚举。

packagemain
import"fmt"
//Direction-Customtypetoholdvalueforweekdayrangingfrom1-4
typeDirectionint
//Declarerelatedconstantsforeachdirectionstartingwithindex1
const(
NorthDirection=iota+1//EnumIndex=1
East//EnumIndex=2
South//EnumIndex=3
West//EnumIndex=4
)
//String-Creatingcommonbehavior-givethetypeaStringfunction
func(dDirection)String()string{
return[...]string{"North","East","South","West"}[d-1]
}
//EnumIndex-Creatingcommonbehavior-givethetypeaEnumIndexfunctio
func(dDirection)EnumIndex()int{
returnint(d)
}
funcmain(){
vardDirection=West
fmt.Println(d)//Print:West
fmt.Println(d.String())//Print:West
fmt.Println(d.EnumIndex())//Print:4
}
结论
枚举是由一组命名常量值组成的数据类型。枚举是一个强大的功能,用途广泛。但是,在 Golang 中,它们的实现方式与大多数其他编程语言大不相同。Golang 不直接支持枚举。我们可以使用iotaand来实现它constants。暂时就这些……继续学习……快乐学习


















![[视频教程] 尚硅谷 Go语言核心编程课程](https://static.kouhao8.com/sucaidashi/xkbb/5850341684cce785a9e4415a3755d65f.png?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)


![[视频教程] 尚硅谷 Go语言核心编程课程](https://static.kouhao8.com/sucaidashi/xkbb/f1d865b99f5d150a767ca3cc237e94c8.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)


![[后端开发] Google资深工程师深度讲解Go语言](https://static.kouhao8.com/sucaidashi/xkbb/49d4b8528a407349b65b347240e23e0b.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)


![[后端开发]Go语言实战流媒体视频网站](https://static.kouhao8.com/sucaidashi/xkbb/5ad4f6e16ce35cdca524d483c3ff194b.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)

相关资源