File tree Expand file tree Collapse file tree 2 files changed +11
-9
lines changed Expand file tree Collapse file tree 2 files changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -115,7 +115,7 @@ func main() {
115
115
116
116
## 3.5 迭代器模式
117
117
118
- ` CycleString ` 有一个迭代器模式的实现 ` CycleStringIterator ` ,通过` Iterator ` 方法来获取:
118
+ ` CycleString ` 有两个迭代器模式的实现 ` CycleStringByteIterator ` 和 ` CycleStringRuneIterator ` ,通过` ByteIterator ` 和 ` RuneIterator ` 方法来获取:
119
119
120
120
``` go
121
121
package main
@@ -129,15 +129,16 @@ import (
129
129
func main () {
130
130
131
131
cycleString := cycle_string.NewCycleString (" CC11001100" )
132
- iterator := cycleString.Iterator ()
132
+ iterator := cycleString.RuneIterator ()
133
+
134
+ fmt.Println (string (iterator.NextN (3 )))
135
+
133
136
for iterator.Next () {
134
137
fmt.Println (string (iterator.Value ()))
135
138
time.Sleep (time.Millisecond * 100 )
136
139
}
137
140
// Output:
138
- // C
139
- // C
140
- // 1
141
+ // CC1
141
142
// 1
142
143
// 0
143
144
// 0
Original file line number Diff line number Diff line change @@ -9,15 +9,16 @@ import (
9
9
func main () {
10
10
11
11
cycleString := cycle_string .NewCycleString ("CC11001100" )
12
- iterator := cycleString .Iterator ()
12
+ iterator := cycleString .RuneIterator ()
13
+
14
+ fmt .Println (string (iterator .NextN (3 )))
15
+
13
16
for iterator .Next () {
14
17
fmt .Println (string (iterator .Value ()))
15
18
time .Sleep (time .Millisecond * 100 )
16
19
}
17
20
// Output:
18
- // C
19
- // C
20
- // 1
21
+ // CC1
21
22
// 1
22
23
// 0
23
24
// 0
You can’t perform that action at this time.
0 commit comments