在go语言中iota比较特殊,是一个被编译器修改的常量,在每一个const关键字出现时被重置为0,然后在下一个const出现之前,每出现一次iota,其所代表的数字就会自动加1
左移运算符”<<”是双目运算符。左移n位就是乘以2的n次方。 其功能把”<<”左边的运算数的各二进位全部左移若干位,由”<<”右边的数指定移动的位数,高位丢弃,低位补0。
右移运算符”>>”是双目运算符。右移n位就是除以2的n次方。 其功能是把”>>”左边的运算数的各二进位全部右移若干位,”>>”右边的数指定移动的位数。
例一:
package iotaimport "fmt"func Test() { const( c1 = iota //c1=0 c2 = iota //c2=1 c3 = iota //c3=2 ) fmt.Println("c1 = ",c1," c2 = ",c2," c3 = ",c3,"\n") const( a = 1 << iota //a = 1 b = 1 << iota //b = 2 c = 1 << iota //c = 4 ) fmt.Println("a = ",a," b = ",b," c = ",c,"\n") const( v1 = iota //v1 = 0 v2 //v2 = 1 v3 //v3 = 2 ) fmt.Println("v1 = ",v1," v2 = ",v2," v3 = ",v3,"\n") const( x = 1 <
例二:
package mainimport ( "fmt")func main() { const ( a2 = 1 << iota //iota = 0 a2 = 1 X 2的0次方 b2 = 3 << iota //iota = 1 b2 = 3 X 2的1次方 c2 //iota = 2 c2 = 3 X 2的2次方 d2 //iota = 3 d2 = 3 X 2的3次方 e2 = 120 >> iota //iota = 4 e2 = 120 / 2的4次方 f2 //iota = 5 f2 = 120 / 2的5次方 ) fmt.Println(a2, b2, c2, d2, e2, f2) //1 6 12 24 7 3}