2015-12-16 06:03:43 +00:00
|
|
|
|
### 3.6.1. iota 常量生成器
|
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
常量聲明可以使用iota常量生成器初始化,它用於生成一組以相似規則初始化的常量,但是不用每行都寫一遍初始化表達式。在一個const聲明語句中,在第一個聲明的常量所在的行,iota將會被置爲0,然後在每一個有常量聲明的行加一。
|
2015-12-21 03:10:24 +00:00
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
下面是來自time包的例子,它首先定義了一個Weekday命名類型,然後爲一週的每天定義了一個常量,從週日0開始。在其它編程語言中,這種類型一般被稱爲枚舉類型。
|
2015-12-21 03:10:24 +00:00
|
|
|
|
|
|
|
|
|
```Go
|
|
|
|
|
type Weekday int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
Sunday Weekday = iota
|
|
|
|
|
Monday
|
|
|
|
|
Tuesday
|
|
|
|
|
Wednesday
|
|
|
|
|
Thursday
|
|
|
|
|
Friday
|
|
|
|
|
Saturday
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
週一將對應0,週一爲1,如此等等。
|
2015-12-21 03:10:24 +00:00
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
我們也可以在複雜的常量表達式中使用iota,下面是來自net包的例子,用於給一個無符號整數的最低5bit的每個bit指定一個名字:
|
2015-12-21 03:10:24 +00:00
|
|
|
|
|
|
|
|
|
```Go
|
|
|
|
|
type Flags uint
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
FlagUp Flags = 1 << iota // is up
|
|
|
|
|
FlagBroadcast // supports broadcast access capability
|
|
|
|
|
FlagLoopback // is a loopback interface
|
|
|
|
|
FlagPointToPoint // belongs to a point-to-point link
|
|
|
|
|
FlagMulticast // supports multicast access capability
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
隨着iota的遞增,每個常量對應表達式1 << iota,是連續的2的冪,分别對應一個bit位置。使用這些常量可以用於測試、設置或清除對應的bit位的值:
|
2015-12-21 03:10:24 +00:00
|
|
|
|
|
2016-01-20 15:25:13 +00:00
|
|
|
|
<u><i>gopl.io/ch3/netflag</i></u>
|
2015-12-21 03:10:24 +00:00
|
|
|
|
```Go
|
|
|
|
|
func IsUp(v Flags) bool { return v&FlagUp == FlagUp }
|
|
|
|
|
func TurnDown(v *Flags) { *v &^= FlagUp }
|
|
|
|
|
func SetBroadcast(v *Flags) { *v |= FlagBroadcast }
|
|
|
|
|
func IsCast(v Flags) bool { return v&(FlagBroadcast|FlagMulticast) != 0 }
|
|
|
|
|
|
|
|
|
|
unc main() {
|
|
|
|
|
var v Flags = FlagMulticast | FlagUp
|
|
|
|
|
fmt.Printf("%b %t\n", v, IsUp(v)) // "10001 true"
|
|
|
|
|
TurnDown(&v)
|
|
|
|
|
fmt.Printf("%b %t\n", v, IsUp(v)) // "10000 false"
|
|
|
|
|
SetBroadcast(&v)
|
|
|
|
|
fmt.Printf("%b %t\n", v, IsUp(v)) // "10010 false"
|
|
|
|
|
fmt.Printf("%b %t\n", v, IsCast(v)) // "10010 true"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
下面是一個更複雜的例子,每個常量都是1024的冪:
|
2015-12-21 03:10:24 +00:00
|
|
|
|
|
|
|
|
|
```Go
|
|
|
|
|
const (
|
|
|
|
|
_ = 1 << (10 * iota)
|
|
|
|
|
KiB // 1024
|
|
|
|
|
MiB // 1048576
|
|
|
|
|
GiB // 1073741824
|
|
|
|
|
TiB // 1099511627776 (exceeds 1 << 32)
|
|
|
|
|
PiB // 1125899906842624
|
|
|
|
|
EiB // 1152921504606846976
|
|
|
|
|
ZiB // 1180591620717411303424 (exceeds 1 << 64)
|
|
|
|
|
YiB // 1208925819614629174706176
|
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
不過iota常量生成規則也有其局限性。例如,它併不能用於産生1000的冪(KB、MB等),因爲Go語言併沒有計算冪的運算符。
|
2015-12-21 03:10:24 +00:00
|
|
|
|
|
2015-12-28 03:14:18 +00:00
|
|
|
|
**練習 3.13:** 編寫KB、MB的常量聲明,然後擴展到YB。
|