#iota的使用

iota是特殊常量,可以认为是一个可以被编译器修改的常量

他实现自增效果

    const (
        ERR1  = iota
        ERR2  
        ERR3  

    )

    fmt.Println(ERR1, ERR2, ERR3)//输出 0 ,1,2
    const (
        a = iota + 1 //1
        b            //2
        c            //3
        d = "ha"     //独立值,iota += 1
        e            //"ha"   iota += 1
        f = 100      //iota +=1
        g            //100  iota +=1
        h = iota     //7,恢复计数
        i            //8
    )
    fmt.Println(a, b, c, d, e, f, g, h) //每一行都计数加1。 比如defg并没有用iota,但到了h时也会加上前面的值。。但是由于我们a有加1,所以恢复计数时如果也加1,那么h就会变成8,i就变成9。..每运行一行iota自动递增8

参考资料:https://www.runoob.com/go/go-constants.html

文档更新时间: 2023-03-26 18:00   作者:young