-
Notifications
You must be signed in to change notification settings - Fork 23
/
iterator_test.go
54 lines (36 loc) · 1.11 KB
/
iterator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package iterator
import (
"fmt"
"testing"
)
//ChildPot 儿童景点
type ChildPot struct {
Name string
}
func (c *ChildPot) Visit() {
fmt.Println("i am: ", c.Name)
}
func TestIterator(t *testing.T) {
scenicArea := ScenicArea{}
scenicArea.AddPot(&ChildPot{Name: "monkey garden"}, &ChildPot{Name: "fairy country"}, &ChildPot{Name: "future space"})
t.Log("pots count:", scenicArea.PotsCount())
potInterator := scenicArea.Iterator()
pot := potInterator.FirstPot()
t.Logf("first pot: %#v\n", pot)
VisitAllPots(potInterator)
t.Log("add a pot", "pot: count", scenicArea.PotsCount())
scenicArea.AddPot(&ChildPot{Name: "virtual world"})
t.Log("pots count:", scenicArea.PotsCount())
//切片变了,所以要重新获取快照.
//如果想进一步了解看以这个例子:
//https://github.com/crazybber/go-fucking-exercise/blob/master/routine/slice_read_write_test.go
potInterator = scenicArea.Iterator()
potInterator.Reset()
VisitAllPots(potInterator)
}
func VisitAllPots(i Iterator) {
for c := i.FirstPot(); !i.IsLastPot(); c = i.Next() {
c.Visit()
fmt.Printf("finish visit pot : %#v\n", c)
}
}