【Go】常量


  1. const

    1
    2
    3
    4
    5
    6
    7
    8
    // const 定义常量
    const length = 100

    const (
    SHANGHAI = 1
    BEIJING = 2
    SHENZHEN = 3
    )
  2. iota

    1
    2
    3
    4
    5
    6
    7
    // iota初始值为0 逐行递增 只能在常量中声明
    const (
    XIAMEN = iota
    QUANZHOU
    ZHANGZHOU
    FUZHOU
    )
  3. 执行结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    func main() {
    fmt.Println("length = ", length)
    fmt.Println("SHANGHAI = ", SHANGHAI)
    fmt.Println("BEIJING = ", BEIJING)
    fmt.Println("SHENZHEN = ", SHENZHEN)
    fmt.Println("XIAMEN = ", XIAMEN)
    fmt.Println("QUANZHOU = ", QUANZHOU)
    fmt.Println("ZHANGZHOU = ", ZHANGZHOU)
    fmt.Println("FUZHOU = ", FUZHOU)
    }