常量

变量赋值

常量

所谓常量,也就是在程序编译阶段就确定下来的值,而程序在运行时无法改变该值,Go 中的常量就是不变量。它们在编译时创建,即便它们可能是函数中定义的局部变量。常量只能是数字、字符(符文)、字符串或布尔值。由于编译时的限制,定义它们的表达式必须也是可被编译器求值的常量表达式。例如 1<<3 就是一个常量表达式,而 math.Sin(math.Pi/4) 则不是,因为对 math.Sin 的函数调用在运行时才会发生。

它的语法如下:

const constantName = value
//如果需要,也可以明确指定常量的类型:
const Pi float32 = 3.1415926

下面是一些常量声明的例子:

const Pi = 3.1415926
const i = 10000
const MaxThread = 10
const prefix = "astaxie_"

Go 常量和一般程序语言不同的是,可以指定相当多的小数位数(例如 200 位),若指定給 float32 自动缩短为 32bit,指定给 float64 自动缩短为 64bit,详情参考链接

iota

在 Go 中,枚举常量使用枚举器 iota 创建。由于 iota 可为表达式的一部分,而表达式可以被隐式地重复,这样也就更容易构建复杂的值的集合了。每次 const 出现时,都会让 iota 初始化为 0。

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

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

type Stereotype int

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

type ByteSize float64

const (
    // 通过赋予空白标识符来忽略第一个值
    _           = iota // ignore first value by assigning to blank identifier
    KB ByteSize = 1 << (10 * iota)
    MB
    GB
    TB
    PB
    EB
    ZB
    YB
)

type Timezone int

const (
    // iota: 0, EST: -5
    EST Timezone = -(5 + iota)

    // iota: 1, CST: -6
    CST

    // iota: 2, MST: -7
    MST

    // iota: 3, MST: -8
    PST
)

重置

// iota reset: it will be 0.
const (
    Zero = iota  // Zero = 0
    One          // One = 1
)
// iota reset: will be 0 again
const (
    Two = iota   // Two = 0
)
// iota: reset
const Three = iota // Three = 0

值筛选

type Timezone int
const (
    // iota: 0, EST: -5
    EST Timezone = -(5 + iota)
    // _ is the blank identifier
    // iota: 1
    _
    // iota: 2, MST: -7
    MST
    // iota: 3, MST: -8
    PST
)
type Timezone int
const (
    // iota: 0, EST: -5
    EST Timezone = -(5 + iota)
    // On a comment or an empty
    // line, iota will not
    // increase
    // iota: 1, CST: -6
    CST
    // iota: 2, MST: -7
    MST
    // iota: 3, MST: -8
    PST
)
type Even bool
const (
    // 0 % 2 == 0 ==> Even(true)
    a = Even(iota % 2 == 0)
    // 1 % 2 == 0 ==> Even(false)
    b
    // 2 % 2 == 0 ==> Even(true)
    c
    // 3 % 2 == 0 ==> Even(false)
    d
)

单行多个 iota:

const (
    // Active = 0, Moving = 0,
    // Running = 0
    Active, Moving, Running = iota, iota, iota
    // Passive = 1, Stopped = 1,
    // Stale = 1
    Passive, Stopped, Stale
)

const (
    // Active = 0, Running = 100
    Active, Running = iota, iota + 100
    // Passive = 1, Stopped = 101
    Passive, Stopped
    // You can't declare like this.
    // The last expression will be
    // repeated
    CantDeclare
    // But, you can reset
    // the last expression
    Reset = iota
    // You can use any other
    // expression even without iota
    AnyOther = 10
)

位操作

type Month int
const (
    // 1 << 0 ==> 1
    January Month = 1 << iota
    February   // 1 << 1 ==> 2
    March      // 1 << 2 ==> 4
    April      // 1 << 3 ==> 8
    May        // 1 << 4 ==> 16
    June       // ...
    July
    August
    September
    October
    November
    December

    // Break the iota chain here.
    // AllMonths will have only
    // the assigned month values,
    // not the iota's.
    AllMonths = January | February |
        March | April | May | June |
        July | August | September |
        October | November |
        December
)
上一页
下一页