「Golang系列」可視化圖解 Go Enums 和 iota(二)

每天三分鐘,知識更輕鬆。
歡迎關注同名微信公眾賬號極客24h。
「Golang系列」可視化圖解 Go Enums 和 iota(二)

基本表達式

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
)

以“未知”開始,以確保枚舉已初始化。

總結

大結局!


分享到:


相關文章: