「Golang系列」可视化图解 Go Enums 和 iota(二)

「Golang系列」可视化图解 Go Enums 和 iota(二)每天三分钟,知识更轻松。欢迎关注同名微信公众账号极客24h。

欢迎大家来到IT世界,在知识的湖畔探索吧!

每天三分钟,知识更轻松。
欢迎关注同名微信公众账号极客24h。

欢迎大家来到IT世界,在知识的湖畔探索吧!

「Golang系列」可视化图解 Go Enums 和 iota(二)

基本表达式

欢迎大家来到IT世界,在知识的湖畔探索吧!type Timezone intconst ( // iota: 0, EST: -5  EST Timezone = -(5 + iota)  // iota: 1, CST: -6 CST  // iota: 2, MST: -7 MST  // iota: 3, MST: -8 PST)

重置iota

// iota reset: it will be 0.const ( Zero = iota // Zero = 0 One // One = 1)// iota reset: will be 0 againconst ( Two = iota // Two = 0)// iota: resetconst Three = iota // Three = 0

跳过一些值

欢迎大家来到IT世界,在知识的湖畔探索吧!type Timezone intconst ( // iota: 0, EST: -5 EST Timezone = -(5 + iota) // _ is the blank identifier // iota: 1 _ // iota: 2, MST: -7 MST // iota: 3, MST: -8 PST)

关于iota在评论和空白行上的行为

type Timezone intconst ( // iota: 0, EST: -5 EST Timezone = -(5 + iota) // On a comment or an empty // line, iota will not  // increase // iota: 1, CST: -6 CST // iota: 2, MST: -7 MST // iota: 3, MST: -8 PST)

在中间使用iota

const ( One = 1 Two = 2 // Three = 2 + 1 => 3 // iota in the middle Three = iota + 1 // Four = 3 + 1 => 4 Four)

单行多个iota

const ( // Active = 0, Moving = 0, // Running = 0 Active, Moving, Running = iota, iota, iota // Passive = 1, Stopped = 1, // Stale = 1 Passive, Stopped, Stale)

在同一行中,所有常量将获得相同的iota值。

const (
 // Active = 0, Running = 100
 Active, Running = iota, iota + 100
 // Passive = 1, Stopped = 101
 Passive, Stopped
 // You can't declare like this.
 // The last expression will be
 // repeated
 CantDeclare
 // But, you can reset 
 // the last expression
 Reset = iota
 // You can use any other
 // expression even without iota
 AnyOther = 10
)

重复和取消表达式

const (
 // iota: 0, One: 1 (type: int64)
 One int64 = iota + 1
 // iota: 1, Two: 2 (type: int64)
 // Two will be declared as if:
 // Two int64 = iota + 1
 Two
 // iota: 2, Four: 4 (type: int32)
 Four int32 = iota + 2
 // iota: 3, Five: 5 (type: int32)
 // Five will be declared as if:
 // Five int32 = iota + 2
 Five
 // (type: int)
 Six = 6
 // (type: int)
 // Seven will be declared as if:
 // Seven = 6
 Seven
)

最后使用的表达式和类型将被重复

奇数和偶数

type Even bool
const (
 // 0 % 2 == 0 ==> Even(true)
 a = Even(iota % 2 == 0)
 // 1 % 2 == 0 ==> Even(false)
 b
 // 2 % 2 == 0 ==> Even(true)
 c
 // 3 % 2 == 0 ==> Even(false)
 d
)

倒数

const (
 max = 10
)
const (
 a = (max - iota) // 10
 b // 9
 c // 8
)

生成字母

const (
 // string will convert the
 // expression into string.
 //
 // or, it'll assign character
 // codes.
 a = string(iota + 'a') // a
 b // b
 c // c
 d // d
 e // e
)

按位运算

type Month int
const (
 // 1 << 0 ==> 1
 January Month = 1 << iota
 February // 1 << 1 ==> 2
 March // 1 << 2 ==> 4
 April // 1 << 3 ==> 8
 May // 1 << 4 ==> 16
 June // ...
 July
 August
 September
 October
 November
 December
 // Break the iota chain here.
 // AllMonths will have only
 // the assigned month values, 
 // not the iota's.
 AllMonths = January | February |
 March | April | May | June |
 July | August | September |
 October | November |
 December
)

注意0值

type Activity int
const (
 Sleeping = iota
 Walking
 Running
)
func main() {
 var activity Activity
 // activity initialized to 
 // its zero-value of int
 // which is Sleeping
}

iota + 1 小技巧

const (
 Sleeping = iota + 1
 Walking
 Running
)
func main() {
 var activity Activity
 // activity will be zero, 
 // so it's not initialized
 activity = Sleeping
 // now you know that it's been
 // initialized
}

使用“ iota + 1”来确保枚举类型已初始化。

未知状态模式

const (
 Unknown = iota
 Sleeping
 Walking
 Running
)

以“未知”开始,以确保枚举已初始化。

总结

大结局!

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/34027.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信