四:go常量,iota

10:常量
从形式上可以分为显式和隐式
const name string = "leyangjun"  //显式
const myName = "我的名字"         //隐式

组合:
const(
   cat string = "猫"
   dog = "狗"
)

单行定义多个:
const apple,banana string = "苹果","香蕉"
const a,b = 1,"wa ha ha"
const bananaLen = len(banana)   -> fmt.Print(bananaLen) 输出6,一个中文占3个字节(UTF8)



iota的使用:
iota在const关键字出现的时候将被重置为0
每新增一行iota将会加1,(计数)
const a = iota
const b = iota    //a,b的常量值都重置为0

const(
   a = iota      //0
   b = iota      //1 ,计数开始加1
)

跳值使用法:
const(
   a = iota      //0
   _          //1
   b = iota      //2 ,跳值使用通过下划线来完成
)

插队使用法:
const(
   a = iota      //0
   b = 3.14     //3.14
   c = iota      //2,直接变成2
)


兴趣输出:
const(
   a = iota * 2  //0
   b = iota     //1
   c = iota      //2
)

const(
   a = iota * 2  //0
   b          //2
   c           //4 ,所有的值都 乘于2
)

const(
   a = iota * 2  //0
   b = iota * 3  //3
   c           //6
   d             //9 隐式使用法
)


//单行使用法
const(
   a,b = iota,iota+3  //0,3(同一行iota值是不加的)
   c,d                //1,4 (c引用的是iota,d引用的是iota+3)
   f = iota          //2 (恢复计数)
)

猜你喜欢

转载自blog.csdn.net/leyangjun/article/details/83615944