iota定义常量
作者:互联网
iota
1 //iota作用会自动递增,引用自己定义的表达式。 2 3 4 //定义所有星期,从0到6 5 package main 6 7 import "fmt" 8 9 const ( 10 Sunday = iota //重点 11 Monday 12 Tuesday 13 Wednesday 14 Thursday 15 Friday 16 Saturday 17 ) 18 19 func main() { 20 21 //从第二个开始,自动引用排头兵的表达式,但iota逐一递增. 22 fmt.Println(Sunday, Monday, Tuesday) //0 1 2 23 24 }
1 ////定义所有星期,从1到7 2 package main 3 4 import "fmt" 5 6 const ( 7 Monday = iota +1 //0 +1 = 1 //重点 8 Tuesday 9 Wednesday 10 Thursday 11 Friday 12 Saturday 13 Sunday 14 ) 15 16 func main() { 17 18 //从第二个开始,自动引用排头兵的表达式,但iota逐一递增. 19 fmt.Println(Monday, Tuesday) //1 2 20 21 }
1 //定义5大常任理事国编号100-500 2 3 package main 4 5 import "fmt" 6 7 const ( 8 USA = (iota + 1) * 100 //重点 9 China 10 Russia 11 Britain 12 France 13 ) 14 15 func main() { 16 17 fmt.Println(USA, China, Russia, Britain, France) //100 200 300 400 500 18 19 }
标签:定义,Monday,fmt,Tuesday,main,iota,常量 来源: https://www.cnblogs.com/chaoyangxu/p/11798956.html