Show Your Codepackage main
import "fmt"
//使用const 定义枚举
const (
bj = 1
sh = 2
sz = 3
)
//可以使用关键iota,每行iota都会累加(注意重点是每行)、第一行的默认值是0
const (
nj = iota
tj
wh
)
//iota 携带计算方法到下一行 直到遇到新的计算方法
const (
an = iota * 10
xc //iota =1 nj=iota * 10 nj=10
hd //iota =2 nj=iota * 10 nj=10
)
const (
a, b = iota + 1, iota + 2 //iota =0 a= iota+1 b= iota+2 a =1 b=2
c, d //iota = 1 c= iota+1 d= iota+2 c=2 c=2 b=3
g, h = iota * 2, iota * 3 //iota = 2 g= iota*2 h= iota*3 g=4 h=6
i, k //iota=3 k=iota*2 k=iota*3 i=6 k=9
)
func main() {
//常量只读,不可以修改
const a int = 10
fmt.Println("a= ", a)
//const 枚举
fmt.Println("bj= ", bj)
fmt.Println("sh= ", sh)
fmt.Println("sz= ", sz)
//const 和 iota
fmt.Println("nj= ", nj)
fmt.Println("tj= ", tj)
fmt.Println("wh= ", wh)
//iota 计算法方法
fmt.Println("an= ", an)
fmt.Println("xc= ", xc)
fmt.Println("hd= ", hd)
//复杂的iota
fmt.Println("a= ", a, "b= ", b)
fmt.Println("c= ", c, "d= ", d)
fmt.Println("g= ", g, "h= ", h)
fmt.Println("i= ", i, "k= ", k)
}