Skip to content

Commit ca32f5a

Browse files
committed
精校:4.8-5.1
1 parent 0227814 commit ca32f5a

File tree

5 files changed

+203
-163
lines changed

5 files changed

+203
-163
lines changed

eBook/04.7.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func main() {
273273
GO1 - The ABC of Go - 25 -
274274
sl2 joined by ;: GO1;The ABC of Go;25
275275

276-
其它有关字符串操作的文档请参阅官方文档 [http://golang.org/pkg/strings/](http://golang.org/pkg/strings/)**译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/strings/)** )。
276+
其它有关字符串操作的文档请参阅 [官方文档](http://golang.org/pkg/strings/)**译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/strings/)** )。
277277

278278
## 4.7.11 从字符串中读取内容
279279

@@ -341,7 +341,7 @@ func main() {
341341

342342
在第 5.1 节,我们将会利用 if 语句来对可能出现的错误进行分类处理。
343343

344-
更多有关该包的讨论,请参阅官方文档 [http://golang.org/pkg/strconv/](http://golang.org/pkg/strconv/)**译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/strconv/)** )。
344+
更多有关该包的讨论,请参阅 [官方文档](http://golang.org/pkg/strconv/)**译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/strconv/)** )。
345345

346346
## 链接
347347

eBook/04.8.md

+37-33
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,48 @@ Duration 类型表示两个连续时刻所相差的纳秒数,类型为 int64
99
包中的一个预定义函数 `func (t Time) Format(layout string) string` 可以根据一个格式化字符串来将一个时间 t 转换为相应格式的字符串,你可以使用一些预定义的格式,如:`time.ANSIC``time.RFC822`
1010

1111
一般的格式化设计是通过对于一个标准时间的格式化描述来展现的,这听起来很奇怪,但看下面这个例子你就会一目了然:
12-
13-
fmt.Println(t.Format("02 Jan 2006 15:04"))
12+
13+
```go
14+
fmt.Println(t.Format("02 Jan 2006 15:04"))
15+
```
1416

1517
输出:
1618

1719
21 Jul 2011 10:31
1820

19-
其它有关时间操作的文档请参阅官方文档 [http://golang.org/pkg/time/](http://golang.org/pkg/time/)**译者注:国内用户可访问 [http://docs.studygolang.com/pkg/time/](http://docs.studygolang.com/pkg/time/)** )。
20-
21-
Example 4.20 [time.go](examples/chapter_4/time.go)
22-
23-
package main
24-
import (
25-
"fmt"
26-
"time"
27-
)
28-
29-
var week time.Duration
30-
func main() {
31-
t := time.Now()
32-
fmt.Println(t) // e.g. Wed Dec 21 09:52:14 +0100 RST 2011
33-
fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year())
34-
// 21.12.2011
35-
t = time.Now().UTC()
36-
fmt.Println(t) // Wed Dec 21 08:52:14 +0000 UTC 2011
37-
fmt.Println(time.Now()) // Wed Dec 21 09:52:14 +0100 RST 2011
38-
// calculating times:
39-
week = 60 * 60 * 24 * 7 * 1e9 // must be in nanosec
40-
week_from_now := t.Add(week)
41-
fmt.Println(week_from_now) // Wed Dec 28 08:52:14 +0000 UTC 2011
42-
// formatting times:
43-
fmt.Println(t.Format(time.RFC822)) // 21 Dec 11 0852 UTC
44-
fmt.Println(t.Format(time.ANSIC)) // Wed Dec 21 08:56:34 2011
45-
fmt.Println(t.Format("02 Jan 2006 15:04")) // 21 Dec 2011 08:52
46-
s := t.Format("20060102")
47-
fmt.Println(t, "=>", s)
48-
// Wed Dec 21 08:52:14 +0000 UTC 2011 => 20111221
49-
}
21+
其它有关时间操作的文档请参阅 [官方文档](http://golang.org/pkg/time/)**译者注:国内用户可访问 [该页面](http://docs.studygolang.com/pkg/time/)** )。
22+
23+
示例 4.20 [time.go](examples/chapter_4/time.go)
24+
25+
```go
26+
package main
27+
import (
28+
"fmt"
29+
"time"
30+
)
31+
32+
var week time.Duration
33+
func main() {
34+
t := time.Now()
35+
fmt.Println(t) // e.g. Wed Dec 21 09:52:14 +0100 RST 2011
36+
fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year())
37+
// 21.12.2011
38+
t = time.Now().UTC()
39+
fmt.Println(t) // Wed Dec 21 08:52:14 +0000 UTC 2011
40+
fmt.Println(time.Now()) // Wed Dec 21 09:52:14 +0100 RST 2011
41+
// calculating times:
42+
week = 60 * 60 * 24 * 7 * 1e9 // must be in nanosec
43+
week_from_now := t.Add(week)
44+
fmt.Println(week_from_now) // Wed Dec 28 08:52:14 +0000 UTC 2011
45+
// formatting times:
46+
fmt.Println(t.Format(time.RFC822)) // 21 Dec 11 0852 UTC
47+
fmt.Println(t.Format(time.ANSIC)) // Wed Dec 21 08:56:34 2011
48+
fmt.Println(t.Format("02 Jan 2006 15:04")) // 21 Dec 2011 08:52
49+
s := t.Format("20060102")
50+
fmt.Println(t, "=>", s)
51+
// Wed Dec 21 08:52:14 +0000 UTC 2011 => 20111221
52+
}
53+
```
5054

5155
输出的结果已经写在每行 `//` 的后面。
5256

eBook/04.9.md

+55-43
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88

99
Go 语言的取地址符是 `&`,放到一个变量前使用就会返回相应变量的内存地址。
1010

11-
下面的代码片段(Example 4.9 [pointer.go](examples/chapter_4/pointer.go))可能输出 `An integer: 5, its location in memory: 0x6b0820`(这个值随着你每次运行程序而变化)。
12-
13-
var i1 = 5
14-
fmt.Printf("An integer: %d, it's location in memory: %p\n", i1, &i1)
11+
下面的代码片段(示例 4.9 [pointer.go](examples/chapter_4/pointer.go))可能输出 `An integer: 5, its location in memory: 0x6b0820`(这个值随着你每次运行程序而变化)。
12+
13+
```go
14+
var i1 = 5
15+
fmt.Printf("An integer: %d, it's location in memory: %p\n", i1, &i1)
16+
```
1517

1618
这个地址可以存储在一个叫做指针的特殊数据类型中,在本例中这是一个指向 int 的指针,即 `i1`:此处使用 *int 表示。如果我们想调用指针 intP,我们可以这样声明它:
17-
18-
var intP *int
19+
20+
```go
21+
var intP *int
22+
```
1923

2024
然后使用 `intP = &i1` 是合法的,此时 intP 指向 i1。
2125

@@ -39,20 +43,22 @@ intP 存储了 i1 的内存地址;它指向了 i1 的位置,它引用了变
3943

4044
现在,我们应当能理解 pointer.go 中的整个程序和他的输出:
4145

42-
Example 4.21 [pointer.go](examples/chapter_4/pointer.go):
43-
44-
package main
45-
import "fmt"
46-
func main() {
47-
var i1 = 5
48-
fmt.Printf("An integer: %d, its location in memory: %p\n", i1, &i1)
49-
var intP *int
50-
intP = &i1
51-
fmt.Printf("The value at memory location %p is %d\n", intP, *intP)
52-
}
46+
示例 4.21 [pointer.go](examples/chapter_4/pointer.go):
47+
48+
```go
49+
package main
50+
import "fmt"
51+
func main() {
52+
var i1 = 5
53+
fmt.Printf("An integer: %d, its location in memory: %p\n", i1, &i1)
54+
var intP *int
55+
intP = &i1
56+
fmt.Printf("The value at memory location %p is %d\n", intP, *intP)
57+
}
58+
```
5359

5460
输出:
55-
61+
5662
An integer: 5, its location in memory: 0x24f0820
5763
The value at memory location 0x24f0820 is 5
5864

@@ -64,18 +70,20 @@ Example 4.21 [pointer.go](examples/chapter_4/pointer.go):
6470

6571
它展示了分配一个新的值给 *p 并且更改这个变量自己的值(这里是一个字符串)。
6672

67-
Example 4.22 [string_pointer.go](examples/chapter_4/string_pointer.go)
68-
69-
package main
70-
import "fmt"
71-
func main() {
72-
s := "good bye"
73-
var p *string = &s
74-
*p = "ciao"
75-
fmt.Printf("Here is the pointer p: %p\n", p) // prints address
76-
fmt.Printf("Here is the string *p: %s\n", *p) // prints string
77-
fmt.Printf("Here is the string s: %s\n", s) // prints same string
78-
}
73+
示例 4.22 [string_pointer.go](examples/chapter_4/string_pointer.go)
74+
75+
```go
76+
package main
77+
import "fmt"
78+
func main() {
79+
s := "good bye"
80+
var p *string = &s
81+
*p = "ciao"
82+
fmt.Printf("Here is the pointer p: %p\n", p) // prints address
83+
fmt.Printf("Here is the string *p: %s\n", *p) // prints string
84+
fmt.Printf("Here is the string s: %s\n", s) // prints same string
85+
}
86+
```
7987

8088
输出:
8189

@@ -92,10 +100,12 @@ Example 4.22 [string_pointer.go](examples/chapter_4/string_pointer.go)
92100
**注意事项**
93101

94102
你不能得到一个文字或常量的地址,例如:
95-
96-
const i = 5
97-
ptr := &i //error: cannot take the address of i
98-
ptr2 := &10 //error: cannot take the address of 10
103+
104+
```go
105+
const i = 5
106+
ptr := &i //error: cannot take the address of i
107+
ptr2 := &10 //error: cannot take the address of 10
108+
```
99109

100110
所以说,Go 语言和 C、C++ 以及 D 语言这些低级(系统)语言一样,都有指针的概念。但是对于经常导致 C 语言内存泄漏继而程序崩溃的指针运算(所谓的指针算法,如:`pointer+2`,移动指针指向字符串的字节数或数组的某个位置)是不被允许的。Go 语言中的指针保证了内存安全,更像是 Java、C# 和 VB.NET 中的引用。
101111

@@ -111,15 +121,17 @@ Example 4.22 [string_pointer.go](examples/chapter_4/string_pointer.go)
111121

112122
对一个空指针的反向引用是不合法的,并且会使程序崩溃:
113123

114-
Example 4.23 [testcrash.go](examples/chapter_4/testcrash.go):
115-
116-
package main
117-
func main() {
118-
var p *int = nil
119-
*p = 0
120-
}
121-
// in Windows: stops only with: <exit code="-1073741819" msg="process crashed"/>
122-
// runtime error: invalid memory address or nil pointer dereference
124+
示例 4.23 [testcrash.go](examples/chapter_4/testcrash.go):
125+
126+
```go
127+
package main
128+
func main() {
129+
var p *int = nil
130+
*p = 0
131+
}
132+
// in Windows: stops only with: <exit code="-1073741819" msg="process crashed"/>
133+
// runtime error: invalid memory address or nil pointer dereference
134+
```
123135

124136
**问题 4.2** 列举 Go 语言中 * 号的所有用法。
125137

eBook/05.0.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
到目前为止,我们看到的都是 Go 程序都是从 main() 函数开始执行,然后按顺序执行该函数体中的代码。但我们经常会需要只有在满足一些特定情况时才执行某些代码,也就是说在代码里进行条件判断。针对这种需求,Go 提供了下面这些条件结构和分支结构:
44

5-
if-else 结构
6-
switch 结构
7-
select 结构,用于 channel 的选择(第 14.4 节)
5+
- if-else 结构
6+
- switch 结构
7+
- elect 结构,用于 channel 的选择第 14.4 节
88

99
可以使用迭代或循环结构来重复执行一次或多次某段代码(任务):
1010

11-
for (range) 结构
11+
- for (range) 结构
1212

1313
一些如 `break``continue` 这样的关键字可以用于中途改变循环的状态。
1414

0 commit comments

Comments
 (0)