Skip to content

Commit 507c6de

Browse files
authored
Update errata.md
1 parent 983c095 commit 507c6de

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

content/errata.md

+53-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,59 @@ title: 勘误表
5050

5151
## 第 73 页
5252

53-
- 运行结果有问题:
54-
55-
<img width="955" alt="image" src="https://user-images.githubusercontent.com/7698088/168478573-8e392460-b602-42d8-9aba-0d7a538f17b1.png">
56-
53+
- 示例代码和运行结果修正
54+
55+
示例代码:
56+
57+
```golang
58+
package main
59+
60+
import (
61+
"fmt"
62+
"time"
63+
)
64+
65+
type Student struct {
66+
name string
67+
age int
68+
}
69+
70+
var s = Student{name: "qcrao", age: 18}
71+
var g = &s
72+
73+
func modifyUser(pu *Student) {
74+
fmt.Println("modifyUser received value:", pu)
75+
pu.name = "Old Old qcrao"
76+
pu.age = 200
77+
}
78+
79+
func printUser(u <-chan *Student) {
80+
time.Sleep(2 * time.Second)
81+
fmt.Println("printUser get:", <-u)
82+
}
83+
84+
func main() {
85+
c := make(chan *Student, 5)
86+
c <- g
87+
fmt.Println(g)
88+
// modify g
89+
g = &Student{name: "Old qcrao", age: 100}
90+
go printUser(c)
91+
go modifyUser(g)
92+
time.Sleep(5 * time.Second)
93+
fmt.Println(g)
94+
}
95+
96+
```
97+
98+
运行结果:
99+
100+
```shell
101+
&{qcrao 18}
102+
modifyUser received value: &{Old qcrao 100}
103+
printUser get: &{qcrao 18}
104+
&{Old Old qcrao 200}
105+
```
57106

58107
- 图中变量标注有误:
59108

0 commit comments

Comments
 (0)