#4.3 常量 ( ***译者注:由于 Go 语言版本更替,本节中的相关内容经原作者同意将被直接替换而不作另外说明*** ) A constant const contains data which does not change. 常量 const 用于存储不能改变的数据。 This data can only be of type boolean, number (integer, float or complex) or string. 常量的数据类型只能是布尔型,数值型(整型,浮点型或复数型)或字符串型。 It is defined as follows: `const identifier [type] = value` , for example: `const Pi = 3.14159` 像如下定义:`const identifier [type] = value` ,例如:`const Pi = 3.14159` The type specifier `[type]` is optional, the compiler can implicitly derive the type from the value. 类型说明符 `[type]` 是可选项,编译器可以从值中隐式推导出类型。 Explicit typing example: `const b string = "abc"` Implicit typing example: `const b = "abc"` 显示类型定义例子: `const b string = "abc"` 隐式类型定义例子: `const b = "abc"` A value derived from an untyped constant becomes typed when it is used within a context that requires a typed value (otherwise formulated: an untyped constant takes the type needed by its context): 当一个值在一个要求有类型值的上下文中,它需要从无类型推导出类型(规定:一个无类型的常量从上下文中确定其类型) var n int f(n + 5) // untyped numeric constant “5” becomes typed as int Constants must be evaluated at compile time; a const can be defined as a calculation, but all the values necessary for the calculation must be available at compile time. 常量必须在编译时能够被推导出;一个常量可以用一个计算定义,但是所有需要计算的值在编译期必须可用。 So this is ok: `const c1 = 2/3` this is NOT: `const c2 = getNumber() // gives the build error: getNumber() used as value` 所以这样是正确的: `const c1 = 2/3` 这样是**错误**的: `const c2 = getNumber() // 得到构建错误: getNumber() used as value` Numeric constants have no size or sign, can be of arbitrary high precision and do no overflow: 数字常量没有大小和符号的,可以是任意精度而且不会溢出: const Ln2= 0.693147180559945309417232121458\ 176568075500134360255254120680009 const Log2E= 1/Ln2 // this is a precise reciprocal const Billion = 1e9 // float constant const hardEight = (1 << 100) >> 97 As demonstrated `\` can be used as a continuation character in a constant. 如上例证明 `\` 可以被用作常量的连字符。 In contrast to numeric variables of different types, with constants you don’t have to worry about conversions: they are like ideal numbers. 相比于数字变量的不同类型,使用常量你不必担心转型:他们就像是理想中的数字。 Constants can overflow only when they are assigned to a numeric variable with too little precision to represent the value, this results in a compile error. Multiple assignment is allowed, like in: 常量会溢出,只有当他们被分配的数字变量精度太少,这将导致一个编译错误。多重赋值是允许的,比如: const beef, two, c = “meat”, 2, “veg” const Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = 1, 2, 3, 4, 5, 6 const ( Monday, Tuesday, Wednesday = 1, 2, 3 Thursday, Friday, Saturday = 4, 5, 6 ) Constants can be used for enumerations: 常量可以用作枚举: const ( Unknown = 0 Female = 1 Male = 2 ) Unknown, Female, Male are now aliases for 0, 1 and 2. They can in effect be used to test for these values, like in a switch / case construct (§ 5.3). Unknown, Female, Male 现在是 0, 1 和 2 的别名. 他们可以使用去测试实际值,比如使用一个 switch / case 结构 (§ 5.3). In such cases, the value iota can be used to enumerate the values: 在这个例子中,`iota` 可以被用作枚举值: const ( a = iota b = iota c = iota ) The first use of iota gives 0, whenever iota is used again on a new line, its value is incremented by 1; so a=0, b=1, c=2. This can be shortened to: 第一个 `iota` 等于 0,无论 `iota` 是否在新的一行使用,它的值都会自动加 1;所以 `a=0, b=1, c=2` 。可以简写为: const ( a = iota b c ) iota can also be used in an expression, like iota + 50. A new const block or declaration initializes iota back to 0. `iota` 也可以用在表达式中,如 `iota + 50`。一个新的常量块或声明将 `iota` 重新初始化为 0。 Of course, the value of a constant cannot change during the execution of the program; doing so is prevented by a compiler error: cannot assign to value, where value is the value of the constant. 当然,常量的值在程序的执行过程中不能被改变;这么做是为了避免编译错误:当一个值是常量,他不能被赋值。 An example from the time package: the names for the days of the week: `time`包的一个例子:一周的每天的名称: const ( Sunday = iota Monday Tuesday Wednesday Thursday Friday Saturday ) You can give the enumeration a type name like in this example: 你可以给枚举一个类型名,像这个例子: type Color int const ( RED Color = iota // 0 ORANGE // 1 YELLOW // 2 GREEN // .. BLUE INDIGO VIOLET // 6 ) Remark: There is a convention to name constant identifiers with all uppercase letters, like: `const INCHTOwCM = 2.54`; this improves readability and can be used as long as it is not in conflict with the Visibility Rule of §4.2 备注:有一个约定:常量标识符全部使用大写字母,如:`const INCHTOwCM = 2.54`;这增强了可读性并且不会与 §4.2 描述的可见性规则冲突。 ##链接 - [目录](directory.md) - 上一部分:[Go 程序的基本结构和要素](04.2.md) - 下一节:[变量](04.4.md)