diff --git a/eBook/04.1.md b/eBook/04.1.md index 94205c1..16b21b1 100644 --- a/eBook/04.1.md +++ b/eBook/04.1.md @@ -1,6 +1,4 @@ -#4.1 文件名、关键字与标识符 -( ***译者注:由于 Go 语言版本更替,本节中的相关内容经原作者同意将被直接替换而不作另外说明*** ) - +#4.1 文件名、关键字与标识符 Go 的源文件以 `.go` 为后缀名存储在计算机中,这些文件名均由小写字母组成,如 `scanner.go` 。如果文件名由多个部分组成,则使用下划线 `_` 对它们进行分隔,如 `scanner_test.go` 。文件名不包含空格或其他特殊字符。 一个源文件可以包含任意多行的代码,Go 本身没有对源文件的大小进行限制。 diff --git a/eBook/04.2.md b/eBook/04.2.md index 10ca102..5e4239a 100644 --- a/eBook/04.2.md +++ b/eBook/04.2.md @@ -1,6 +1,4 @@ #4.2 Go 程序的基本结构和要素 -( ***译者注:由于 Go 语言版本更替,本节中的相关内容经原作者同意将被直接替换而不作另外说明*** ) - Example 4.1 [hello_world.go](examples/chapter_4/hello_world.go) package main @@ -299,5 +297,5 @@ Go 程序的执行(程序启动)顺序如下: ##链接 - [目录](directory.md) -- 上一部分:[文件名、关键字与标识符](04.1.md) +- 上一节:[文件名、关键字与标识符](04.1.md) - 下一节:[常量](04.3.md) diff --git a/eBook/04.3.md b/eBook/04.3.md index 6b585a7..01f4711 100644 --- a/eBook/04.3.md +++ b/eBook/04.3.md @@ -1,50 +1,28 @@ -#4.3 常量 -( ***译者注:由于 Go 语言版本更替,本节中的相关内容经原作者同意将被直接替换而不作另外说明*** ) +#4.3 常量 +常量使用关键字 const 定义,用于存储不会改变的数据。 -A constant const contains data which does not change. +存储在常量中的数据类型只可以是布尔型、数字型(整数型、浮点型和复数)和字符串型。 -常量 const 用于存储不能改变的数据。 +常量的定义格式:`const identifier [type] = value`,例如 `const Pi = 3.14159`。 +在 Go 语言中,你可以省略类型说明符 `[type]`,因为编译器可以根据变量的值来推断其类型。 -This data can only be of type boolean, number (integer, float or complex) or string. +- 显式类型定义: `const b string = "abc"` +- 隐式类型定义: `const b = "abc"` -常量的数据类型只能是布尔型,数值型(整型,浮点型或复数型)或字符串型。 - - -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 + f(n + 5) // 无类型的数字型常量 “5” 它的类型在这里变成了 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. +常量的值必须是能够在编译时就能够确定的;你可以在其赋值表达式中涉及计算过程,但是所有用于计算的值必须在编译期间就能获得。 -常量必须在编译时能够被推导出;一个常量可以用一个计算定义,但是所有需要计算的值在编译期必须可用。 +- 正确的做法:`const c1 = 2/3` +- 错误的做法:`const c2 = getNumber()` // 引发构建错误: `getNumber() used as value` -So this is ok: `const c1 = 2/3` -this is NOT: `const c2 = getNumber() // gives the build error: getNumber() used as value` +( ***译者注:因为在编译期间自定义函数均属于未知,因此无法用于常量的赋值,但内置函数可以使用,如:len()*** ) -所以这样是正确的: `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 @@ -52,17 +30,11 @@ Numeric constants have no size or sign, can be of arbitrary high precision and d 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 @@ -71,9 +43,7 @@ Constants can overflow only when they are assigned to a numeric variable with to Thursday, Friday, Saturday = 4, 5, 6 ) -Constants can be used for enumerations: - -常量可以用作枚举: +常量还可以用作枚举: const ( Unknown = 0 @@ -81,11 +51,7 @@ Constants can be used for enumerations: 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: +现在,数字 0、1 和 2 分别代表未知性别、女性和男性。这些枚举值可以用于测试某个变量或常量的实际值,比如使用 switch/case 结构 (第 5.3 节). 在这个例子中,`iota` 可以被用作枚举值: @@ -97,7 +63,7 @@ In such cases, the value iota can be used to enumerate the values: 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` 。可以简写为: +第一个 `iota` 等于 0,每当 `iota` 在新的一行被使用时,它的值都会自动加 1;所以 `a=0, b=1, c=2` 可以简写为如下形式: const ( a = iota @@ -105,17 +71,13 @@ The first use of iota gives 0, whenever iota is used again on a new line, its va 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 的用法进行直观的文字描述。如希望进一步了解,请观看视频教程 [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming) [第四课:常量与运算符](https://github.com/Unknwon/go-fundamental-programming/blob/master/lecture4/lecture4.md)*** ) -`iota` 也可以用在表达式中,如 `iota + 50`。一个新的常量块或声明将 `iota` 重新初始化为 0。 +`iota` 也可以用在表达式中,如:`iota + 50`。在每遇到一个新的常量块或单个常量声明时, `iota` 都会重置为 0( ***译者注:简单地讲,每遇到一次 const 关键字,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`包的一个例子:一周的每天的名称: +引用 `time` 包中的一段代码作为示例:一周中每天的名称。 const ( Sunday = iota @@ -127,9 +89,7 @@ An example from the time package: the names for the days of the week: Saturday ) -You can give the enumeration a type name like in this example: - -你可以给枚举一个类型名,像这个例子: +你也可以使用某个类型作为枚举常量的类型: type Color int const ( @@ -144,9 +104,9 @@ You can give the enumeration a type name like in this example: 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 描述的可见性规则冲突。 +**注意事项** 作为约定,常量的标识符主要使用大写字母,标识符中各个部分的连接字符可用小写字母以便区分,如:`const INCHTOwCM = 2.54`;这样不仅增强了可读性,而且不会与第 4.2 节中描述的可见性规则冲突。 ##链接 - [目录](directory.md) -- 上一部分:[Go 程序的基本结构和要素](04.2.md) +- 上一节:[Go 程序的基本结构和要素](04.2.md) - 下一节:[变量](04.4.md) diff --git a/eBook/directory.md b/eBook/directory.md index 2589c38..7c68efd 100644 --- a/eBook/directory.md +++ b/eBook/directory.md @@ -22,6 +22,8 @@ ##第二部分:语言的核心结构与技术 - 第4章:基本结构和基本数据类型 - 4.1 [文件名、关键字与标识符](04.1.md) + - 4.2 [Go 程序的基本结构和要素](04.2.md) + - 4.3 [常量](04.3.md) - 第5章:控制结构 - 第6章:函数(function) - 第7章:数组(array)与切片(slice)