Constants

相对于变量,常量是恒定不变的值,多用于定义程序运行期间不会改变的那些值。 常量的声明和变量声明非常类似,只是把var换成了const,常量在定义的时候必须赋值。

  1. const pi = 3.1415
  2. const e = 2.7182

声明了pi和e这两个常量之后,在整个程序运行期间它们的值都不能再发生变化了。

多个常量也可以一起声明:

  1. const (
  2. pi = 3.1415
  3. e = 2.7182
  4. )

const同时声明多个常量时,如果省略了值则表示和上面一行的值相同。 例如:

  1. const (
  2. n1 = 100
  3. n2
  4. n3
  5. )

上面示例中,常量n1、n2、n3的值都是100。

iota

iota是go语言的常量计数器,只能在常量的表达式中使用。

iota在const关键字出现时将被重置为0。

const中每新增一行常量声明将使iota计数一次(iota可理解为const语句块中的行索引)。

使用iota能简化定义,在定义枚举时很有用。

举个例子:

  1. const (
  2. n1 = iota //0
  3. n2 //1
  4. n3 //2
  5. n4 //3
  6. )

使用_跳过某些值

  1. const (
  2. n1 = iota //0
  3. n2 //1
  4. _
  5. n4 //3
  6. )

iota声明中间插队

  1. const (
  2. n1 = iota //0
  3. n2 = 100 //100
  4. n3 = iota //2
  5. n4 //3
  6. )
  7. const n5 = iota //0

定义数量级

(这里的<<表示左移操作,1<<10表示将1的二进制表示向左移10位,也就是由1变成了10000000000,也就是十进制的1024。同理2<<2表示将2的二进制表示向左移2位,也就是由10变成了1000,也就是十进制的8。)

  1. const (
  2. _ = iota
  3. KB = 1 << (10 * iota)
  4. MB = 1 << (10 * iota)
  5. GB = 1 << (10 * iota)
  6. TB = 1 << (10 * iota)
  7. PB = 1 << (10 * iota)
  8. )

多个iota定义在一行

  1. const (
  2. a, b = iota + 1, iota + 2 //1,2
  3. c, d //2,3
  4. e, f //3,4
  5. )

Constants 常量 官方文档

Constants in Go are just that—constant. They are created at compile time, even when defined as locals in functions, and can only be numbers, characters (runes), strings or booleans. Because of the compile-time restriction, the expressions that define them must be constant expressions, evaluatable by the compiler. For instance, 1<<3 is a constant expression, while math.Sin(math.Pi/4) is not because the function call to math.Sin needs to happen at run time.

围棋中的常量就是这个常量。它们是在编译时创建的,甚至在函数中定义为局部变量时也是如此,它们只能是数字、字符(符文)、字符串或布尔值。由于编译时的限制,定义它们的表达式必须是常量表达式,可由编译器计算。例如,1 < 3是一个常量表达式,而数学。Sin (math.Pi/4)并不是因为函数调用了 math。罪恶需要在运行时发生。

In Go, enumerated constants are created using the iota enumerator. Since iota can be part of an expression and expressions can be implicitly repeated, it is easy to build intricate sets of values.

在 Go 中,使用 iota 枚举器创建枚举常量。由于 iota 可以是表达式的一部分,而且表达式可以隐式重复,因此很容易构建复杂的值集。

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

The ability to attach a method such as String to any user-defined type makes it possible for arbitrary values to format themselves automatically for printing. Although you’ll see it most often applied to structs, this technique is also useful for scalar types such as floating-point types like ByteSize.

将 String 等方法附加到任何用户定义类型的能力使得任意值可以自动格式化自身以便打印。尽管您会看到它最常应用于结构,但这种技术对于像 ByteSize 这样的浮点类型等标量类型也很有用。

  1. func (b ByteSize) String() string {
  2. switch {
  3. case b >= YB:
  4. return fmt.Sprintf("%.2fYB", b/YB)
  5. case b >= ZB:
  6. return fmt.Sprintf("%.2fZB", b/ZB)
  7. case b >= EB:
  8. return fmt.Sprintf("%.2fEB", b/EB)
  9. case b >= PB:
  10. return fmt.Sprintf("%.2fPB", b/PB)
  11. case b >= TB:
  12. return fmt.Sprintf("%.2fTB", b/TB)
  13. case b >= GB:
  14. return fmt.Sprintf("%.2fGB", b/GB)
  15. case b >= MB:
  16. return fmt.Sprintf("%.2fMB", b/MB)
  17. case b >= KB:
  18. return fmt.Sprintf("%.2fKB", b/KB)
  19. }
  20. return fmt.Sprintf("%.2fB", b)
  21. }

The expression YB prints as 1.00YB, while ByteSize(1e13) prints as 9.09TB.

表达式 YB 打印为1.00 YB,而 ByteSize (1e13)打印为9.09 TB。

The use here of Sprintf to implement ByteSize’s String method is safe (avoids recurring indefinitely) not because of a conversion but because it calls Sprintf with %f, which is not a string format: Sprintf will only call the String method when it wants a string, and %f wants a floating-point value.

在这里使用 Sprintf 实现 ByteSize 的 String 方法是安全的(可以无限期地避免重复出现) ,不是因为转换,而是因为它使用% f 调用 Sprintf,而% f 不是字符串格式: Sprintf 只在需要字符串时调用 String 方法,而% f 需要浮点值。