Skip to content

Commit eef8f41

Browse files
committed
update
1 parent 8a9282f commit eef8f41

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

workspace/lesson22/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Once struct {
1414

1515
这个结构体只有1个方法Do,参数是要执行的函数。(**注意**:参数是函数类型,而不是函数的返回值,所以只需要把函数名作为参数给到Do即可)
1616

17-
可以看到Do方法的参数**f**这个函数类型没有参数,所以如果要执行的函数f如果需要传递参数就要结合Go的闭包来使用
17+
可以看到Do方法的参数**f**这个函数类型没有参数,所以如果要执行的函数f需要传递参数就要结合Go的闭包来使用
1818

1919
```go
2020
func(o *Once) Do(f func())
@@ -108,7 +108,7 @@ func main() {
108108

109109

110110

111-
## 注意事项
111+
## 注意事项
112112

113113
* Once变量作为函数参数传递时,只能传指针,不能传值。传值给函数A的话,对于函数A而言,参数列表里的once形参会是一个新生成的once局部变量,和外部传入的once实参不一样。
114114

workspace/test/example4.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
var battle = make(chan string)
6+
7+
func warrior(name string, done chan struct{}) {
8+
select {
9+
case opponent := <-battle:
10+
fmt.Printf("%s beat %s\n", name, opponent)
11+
case battle <- name:
12+
// I lost :-(
13+
}
14+
done <- struct{}{}
15+
}
16+
17+
func main() {
18+
done := make(chan struct{})
19+
langs := []string{"Go", "C", "C++", "Java", "Perl", "Python"}
20+
for _, l := range langs { go warrior(l, done) }
21+
for _ = range langs { fmt.Println(0);<-done }
22+
}

workspace/test/example5.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type ST struct {
6+
ST2
7+
}
8+
9+
type ST2 struct{
10+
11+
}
12+
13+
func(st *ST2) close(a int) int {
14+
return a
15+
}
16+
17+
func add(a, b int) int{
18+
return a+b
19+
}
20+
21+
func main() {
22+
s := ST{ST2{}}
23+
fmt.Println(s.close(10))
24+
25+
result := add(1, 2)
26+
fmt.Println(&result)
27+
28+
sl := [3]int{1, 2}
29+
fmt.Println(&sl)
30+
fmt.Println(&sl[0:1])
31+
}
32+

0 commit comments

Comments
 (0)