# 彻底搞懂 golang 里的 iota

Wolfogre's Blog · · 2387 次点击 · · 开始浏览

## 一

``````const (
a = iota
b
c
)
``````

``````const (
mutexLocked = 1 << iota
mutexWoken
mutexStarving
mutexWaiterShift = iota
)
``````

``````const a = iota

const (
b = iota
)

const (
c = 10
d = iota
e
f = "hello"
// nothing
g
h = iota
i
j = 0
k
l, m = iota, iota
n, o

p = iota + 1
q
_
r = iota * iota
s
t = r
u
v = 1 << iota
w
x = iota * 0.01
y float32 = iota * 0.01
z
)
``````

## 二

### 第一步：不同 const 定义块互不干扰

``````const a = iota

const (
b = iota
)
``````

### 第二步：所有注释行和空行全部忽略

``````const (
c = 10
d = iota
e
f = "hello"
g
h = iota
i
j = 0
k
l, m = iota, iota
n, o
p = iota + 1
q
_
r = iota * iota
s
t = r
u
v = 1 << iota
w
x = iota * 0.01
y float32 = iota * 0.01
z
)
``````

### 第三步：没有表达式的常量定义复用上一行的表达式

``````const (
c = 10
d = iota
e = iota
f = "hello"
g = "hello"
h = iota
i = iota
j = 0
k = 0
l, m = iota, iota
n, o = iota, iota
p = iota + 1
q = iota + 1
_ = iota + 1
r = iota * iota
s = iota * iota
t = r
u = r
v = 1 << iota
w = 1 << iota
x = iota * 0.01
y float32 = iota * 0.01
z float32 = iota * 0.01
)
``````

### 第四步：从第一行开始，iota 从 0 逐行加一

``````const (
c = 10 // iota = 0
d = iota // iota = 1
e = iota // iota = 2
f = "hello" // iota = 3
g = "hello" // iota = 4
h = iota // iota = 5
i = iota // iota = 6
j = 0 // iota = 7
k = 0 // iota = 8
l, m = iota, iota // iota = 9
n, o = iota, iota // iota = 10
p = iota + 1 // iota = 11
q = iota + 1 // iota = 12
_ = iota + 1 // iota = 13
r = iota * iota // iota = 14
s = iota * iota // iota = 15
t = r // iota = 16
u = r // iota = 17
v = 1 << iota // iota = 18
w = 1 << iota // iota = 19
x = iota * 0.01 // iota = 20
y float32 = iota * 0.01 // iota = 21
z float32 = iota * 0.01 // iota = 22
)
``````

### 第五步：替换所有 iota

``````const (
c = 10 // iota = 0
d = 1 // iota = 1
e = 2 // iota = 2
f = "hello" // iota = 3
g = "hello" // iota = 4
h = 5 // iota = 5
i = 6 // iota = 6
j = 0 // iota = 7
k = 0 // iota = 8
l, m = 9, 9 // iota = 9
n, o = 10, 10 // iota = 10
p = 11 + 1 // iota = 11
q = 12 + 1 // iota = 12
_ = 13 + 1 // iota = 13
r = 14 * 14 // iota = 14
s = 15 * 15 // iota = 15
t = r // iota = 16
u = r // iota = 17
v = 1 << 18 // iota = 18
w = 1 << 19 // iota = 19
x = 20 * 0.01 // iota = 20
y float32 = 21 * 0.01 // iota = 21
z float32 = 22 * 0.01 // iota = 22
)
``````

## 三

1. 不同 const 定义块互不干扰；
2. 所有注释行和空行全部忽略；
3. 没有表达式的常量定义复用上一行的表达式；
4. 从第一行开始，iota 从 0 逐行加一；
5. 替换所有 iota。

## 附

``````package main

import (
"fmt"
)

const a = iota

const (
b = iota
)

const (
c = 0
d = iota
e
f = "hello"
// nothing
g
h = iota
i
j = 0
k
l, m = iota, iota
n, o

p = iota + 1
q
_
r = iota * iota
s
t = r
u
v = 1 << iota
w
x = iota * 0.01
y float32 = iota * 0.01
z
)

func main() {
fmt.Printf("a : %T = %v\n", a, a)
fmt.Printf("b : %T = %v\n", b, b)
fmt.Printf("c : %T = %v\n", c, c)
fmt.Printf("d : %T = %v\n", d, d)
fmt.Printf("e : %T = %v\n", e, e)
fmt.Printf("f : %T = %v\n", f, f)
fmt.Printf("g : %T = %v\n", g, g)
fmt.Printf("h : %T = %v\n", h, h)
fmt.Printf("i : %T = %v\n", i, i)
fmt.Printf("j : %T = %v\n", j, j)
fmt.Printf("k : %T = %v\n", k, k)
fmt.Printf("l : %T = %v\n", l, l)
fmt.Printf("m : %T = %v\n", m, m)
fmt.Printf("n : %T = %v\n", n, n)
fmt.Printf("o : %T = %v\n", o, o)
fmt.Printf("p : %T = %v\n", p, p)
fmt.Printf("q : %T = %v\n", q, q)
fmt.Printf("r : %T = %v\n", r, r)
fmt.Printf("s : %T = %v\n", s, s)
fmt.Printf("t : %T = %v\n", t, t)
fmt.Printf("u : %T = %v\n", u, u)
fmt.Printf("v : %T = %v\n", v, v)
fmt.Printf("w : %T = %v\n", w, w)
fmt.Printf("x : %T = %v\n", x, x)
fmt.Printf("y : %T = %v\n", y, y)
fmt.Printf("z : %T = %v\n", z, z)
}
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

## 一

``````const (
a = iota
b
c
)
``````

``````const (
mutexLocked = 1 << iota
mutexWoken
mutexStarving
mutexWaiterShift = iota
)
``````

``````const a = iota

const (
b = iota
)

const (
c = 10
d = iota
e
f = "hello"
// nothing
g
h = iota
i
j = 0
k
l, m = iota, iota
n, o

p = iota + 1
q
_
r = iota * iota
s
t = r
u
v = 1 << iota
w
x = iota * 0.01
y float32 = iota * 0.01
z
)
``````

## 二

### 第一步：不同 const 定义块互不干扰

``````const a = iota

const (
b = iota
)
``````

### 第二步：所有注释行和空行全部忽略

``````const (
c = 10
d = iota
e
f = "hello"
g
h = iota
i
j = 0
k
l, m = iota, iota
n, o
p = iota + 1
q
_
r = iota * iota
s
t = r
u
v = 1 << iota
w
x = iota * 0.01
y float32 = iota * 0.01
z
)
``````

### 第三步：没有表达式的常量定义复用上一行的表达式

``````const (
c = 10
d = iota
e = iota
f = "hello"
g = "hello"
h = iota
i = iota
j = 0
k = 0
l, m = iota, iota
n, o = iota, iota
p = iota + 1
q = iota + 1
_ = iota + 1
r = iota * iota
s = iota * iota
t = r
u = r
v = 1 << iota
w = 1 << iota
x = iota * 0.01
y float32 = iota * 0.01
z float32 = iota * 0.01
)
``````

### 第四步：从第一行开始，iota 从 0 逐行加一

``````const (
c = 10 // iota = 0
d = iota // iota = 1
e = iota // iota = 2
f = "hello" // iota = 3
g = "hello" // iota = 4
h = iota // iota = 5
i = iota // iota = 6
j = 0 // iota = 7
k = 0 // iota = 8
l, m = iota, iota // iota = 9
n, o = iota, iota // iota = 10
p = iota + 1 // iota = 11
q = iota + 1 // iota = 12
_ = iota + 1 // iota = 13
r = iota * iota // iota = 14
s = iota * iota // iota = 15
t = r // iota = 16
u = r // iota = 17
v = 1 << iota // iota = 18
w = 1 << iota // iota = 19
x = iota * 0.01 // iota = 20
y float32 = iota * 0.01 // iota = 21
z float32 = iota * 0.01 // iota = 22
)
``````

### 第五步：替换所有 iota

``````const (
c = 10 // iota = 0
d = 1 // iota = 1
e = 2 // iota = 2
f = "hello" // iota = 3
g = "hello" // iota = 4
h = 5 // iota = 5
i = 6 // iota = 6
j = 0 // iota = 7
k = 0 // iota = 8
l, m = 9, 9 // iota = 9
n, o = 10, 10 // iota = 10
p = 11 + 1 // iota = 11
q = 12 + 1 // iota = 12
_ = 13 + 1 // iota = 13
r = 14 * 14 // iota = 14
s = 15 * 15 // iota = 15
t = r // iota = 16
u = r // iota = 17
v = 1 << 18 // iota = 18
w = 1 << 19 // iota = 19
x = 20 * 0.01 // iota = 20
y float32 = 21 * 0.01 // iota = 21
z float32 = 22 * 0.01 // iota = 22
)
``````

## 三

1. 不同 const 定义块互不干扰；
2. 所有注释行和空行全部忽略；
3. 没有表达式的常量定义复用上一行的表达式；
4. 从第一行开始，iota 从 0 逐行加一；
5. 替换所有 iota。

## 附

``````package main

import (
"fmt"
)

const a = iota

const (
b = iota
)

const (
c = 0
d = iota
e
f = "hello"
// nothing
g
h = iota
i
j = 0
k
l, m = iota, iota
n, o

p = iota + 1
q
_
r = iota * iota
s
t = r
u
v = 1 << iota
w
x = iota * 0.01
y float32 = iota * 0.01
z
)

func main() {
fmt.Printf("a : %T = %v\n", a, a)
fmt.Printf("b : %T = %v\n", b, b)
fmt.Printf("c : %T = %v\n", c, c)
fmt.Printf("d : %T = %v\n", d, d)
fmt.Printf("e : %T = %v\n", e, e)
fmt.Printf("f : %T = %v\n", f, f)
fmt.Printf("g : %T = %v\n", g, g)
fmt.Printf("h : %T = %v\n", h, h)
fmt.Printf("i : %T = %v\n", i, i)
fmt.Printf("j : %T = %v\n", j, j)
fmt.Printf("k : %T = %v\n", k, k)
fmt.Printf("l : %T = %v\n", l, l)
fmt.Printf("m : %T = %v\n", m, m)
fmt.Printf("n : %T = %v\n", n, n)
fmt.Printf("o : %T = %v\n", o, o)
fmt.Printf("p : %T = %v\n", p, p)
fmt.Printf("q : %T = %v\n", q, q)
fmt.Printf("r : %T = %v\n", r, r)
fmt.Printf("s : %T = %v\n", s, s)
fmt.Printf("t : %T = %v\n", t, t)
fmt.Printf("u : %T = %v\n", u, u)
fmt.Printf("v : %T = %v\n", v, v)
fmt.Printf("w : %T = %v\n", w, w)
fmt.Printf("x : %T = %v\n", x, x)
fmt.Printf("y : %T = %v\n", y, y)
fmt.Printf("z : %T = %v\n", z, z)
}
``````