您的位置 首页 golang

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

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

基本表达式

type Timezone int
 const  (
 // 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 again
const (
 Two = iota // Two = 0
)
// iota: reset
const Three = iota // Three = 0 

跳过一些值

type Timezone int
const (
 // 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 int
const (
 // 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
) 

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

总结

大结局!

文章来源:智云一二三科技

文章标题:「Golang系列」可视化图解 Go Enums 和 iota(二)

文章地址:https://www.zhihuclub.com/100617.shtml

关于作者: 智云科技

热门文章

网站地图