go语言中iota的一个例子
发布时间:2020-12-16 18:43:25 所属栏目:大数据 来源:网络整理
导读:package mainimport ( "fmt")type BitFlag intconst ( // iota为0,1左移0位 = 1 Active BitFlag = 1 iota // Send = Active = 1 iota,此时iota为1,1左移1位 = 2 Send // Receive = Send = 1 iota,此时iota为2,1左移2位 = 4 Receive)func main() { fmt.P
package main import ( "fmt" ) type BitFlag int const ( // iota为0,1左移0位 = 1 Active BitFlag = 1 << iota // Send <=> Active <=> 1 << iota,此时iota为1,1左移1位 = 2 Send // Receive <=> Send <=> 1 << iota,此时iota为2,1左移2位 = 4 Receive ) func main() { fmt.Println(Active,Send,Receive) } iota是在编译的时候,编译器根据代码中iota与const关键字的位置动态替换的。 package main import ( "fmt" ) const ( //e=0,f=0,g=0 e,f,g = iota,iota,iota ) func main() { fmt.Println(e,g) } 可以将iota理解为const语句的行索引 package main import ( "fmt" ) func main() { fmt.Println(iota) } 编译错误:undefined: iota. iota是预先声明的标识符,但是只能作用在const常量声明里。 我怎么觉得iota这东西是go的私生子,只能被关在某个地方,不同于true/false等这些兄弟,不能访问它。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |