【Go学习】Golang 使用 iota

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xj178926426/article/details/79398466

【Go学习】Golang 使用 iota

iota是golang语言的常量计数器,只能在常量的表达式中使用。
iota在const关键字出现时将被重置为0(const内部的第一行之前),const中每新增一行常量声明将使iota计数一次(iota可理解为const语句块中的行索引)。
使用iota能简化定义,在定义枚举时很有用。

举例如下:

1、iota只能在常量的表达式中使用。

package main

import "fmt"

func main(){
    fmt.Println(iota)
}

编译错误:
这里写图片描述

2、每次 const 出现时,都会让 iota 初始化为0。

package main

import "fmt"


const a = iota // a=0
const (
    b = iota     //b=0
    c            //c=1
)

func main(){
    fmt.Println("a = " , a )
    fmt.Println("b = " , b )
    fmt.Println("c = " , c )
}

运行结果如下:
这里写图片描述

3、自定义类型

自增长常量经常包含一个自定义枚举类型,允许你依靠编译器完成自增设置。

package main

import "fmt"


type Stereotype int

const (
    TypicalNoob Stereotype = iota // 0
    TypicalHipster                // 1
    TypicalUnixWizard             // 2
    TypicalStartupFounder         // 3
)

func main(){
    fmt.Println("TypicalNoob = " , TypicalNoob )
    fmt.Println("TypicalHipster = " , TypicalHipster )
    fmt.Println("TypicalUnixWizard = " , TypicalUnixWizard )
    fmt.Println("TypicalStartupFounder = " , TypicalStartupFounder )
}

运行结果如下:
这里写图片描述

4、可跳过的值

我们可以使用下划线跳过不想要的值。

package main

import "fmt"

type AudioOutput int

const (
    OutMute AudioOutput = iota // 0
    OutMono                    // 1
    OutStereo                  // 2
    _
    _
    OutSurround                // 5
)

func main(){
    fmt.Println("OutMute = " , OutMute )
    fmt.Println("OutMono = " , OutMono )
    fmt.Println("OutStereo = " , OutStereo )
    fmt.Println("OutSurround = " , OutSurround )
}

运行结果如下:
这里写图片描述

5、位掩码表达式

package main

import "fmt"

type Allergen int

const (
    IgEggs Allergen = 1 << iota         // 1 << 0 which is 00000001
    IgChocolate                         // 1 << 1 which is 00000010
    IgNuts                              // 1 << 2 which is 00000100
    IgStrawberries                      // 1 << 3 which is 00001000
    IgShellfish                         // 1 << 4 which is 00010000
)

func main(){
    fmt.Println("IgEggs = " , IgEggs )
    fmt.Println("IgChocolate = " , IgChocolate )
    fmt.Println("IgNuts = " , IgNuts )
    fmt.Println("IgStrawberries = " , IgStrawberries )
    fmt.Println("IgShellfish = " , IgShellfish )
}

运行结果:
这里写图片描述

这个工作是因为当你在一个 const 组中仅仅有一个标示符在一行的时候,它将使用增长的 iota 取得前面的表达式并且再运用它,在 Go 语言的 spec 中, 这就是所谓的隐性重复最后一个非空的表达式列表。

6、定义数量级

package main

import "fmt"

type ByteSize float64

const (
    _           = iota                   // ignore first value by assigning to blank identifier
    KB ByteSize = 1 << (10 * iota) // 1 << (10*1)
    MB                                   // 1 << (10*2)
    GB                                   // 1 << (10*3)
    TB                                   // 1 << (10*4)
    PB                                   // 1 << (10*5)
    EB                                   // 1 << (10*6)
    ZB                                   // 1 << (10*7)
    YB                                   // 1 << (10*8)
)

func main(){
    fmt.Println("KB = " , KB )
    fmt.Println("MB = " , MB )
    fmt.Println("GB = " , GB )
    fmt.Println("TB = " , TB )
    fmt.Println("PB = " , PB )
    fmt.Println("EB = " , EB )
    fmt.Println("ZB = " , ZB )
    fmt.Println("YB = " , YB )

}

运行结果:
这里写图片描述

7、定义在一行的情况

package main

import "fmt"

const (
    Apple, Banana = iota + 1, iota + 2
    Cherimoya, Durian
    Elderberry, Fig
)

func main(){
    fmt.Println("Apple = " , Apple )
    fmt.Println("Banana = " , Banana )
    fmt.Println("Cherimoya = " , Cherimoya )
    fmt.Println("Durian = " , Durian )
    fmt.Println("Elderberry = " , Elderberry )
    fmt.Println("Fig = " , Fig )

}

运行结果如下:
这里写图片描述
说明iota 在下一行增长,而不是立即取得它的引用。

8、中间插队

package main

import "fmt"

const (
    i = iota
    j = 3.14
    k = iota
    l
)
func main(){
    fmt.Println("i = " , i )
    fmt.Println("j = " , j )
    fmt.Println("k = " , k )
    fmt.Println("l = " , l )
}

运行结果如下:
这里写图片描述
这说明中间插入行对itoa的值不会有影响,iota可理解为const语句块中的行索引。

猜你喜欢

转载自blog.csdn.net/xj178926426/article/details/79398466