Skip to content

Commit 11abe1f

Browse files
committed
minor formatting and ordering corrections for iterator example
1 parent dc172e1 commit 11abe1f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

mocking.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ Under test doubles, there are various types like stubs, spies and indeed mocks!
641641

642642
In Go 1.23 [iterators were introduced](https://tip.golang.org/doc/go1.23). We can use iterators in various ways, in this instance we can make a `countdownFrom` iterator, which will return the numbers to countdown in reverse order.
643643

644+
Before we get into how we write custom iterators, let's see how we use them. Rather than writing a fairly imperative looking loop to count down from a number, we can make this code look more expressive by `range`-ing over our custom `countdownFrom` iterator.
645+
644646
```go
645647
func Countdown(out io.Writer, sleeper Sleeper) {
646648
for i := range countDownFrom(3) {
@@ -652,9 +654,7 @@ func Countdown(out io.Writer, sleeper Sleeper) {
652654
}
653655
```
654656

655-
Before we get into how we write custom iterators, let's see how we use it. Rather than writing a fairly imperative looking loop to count down from a number, we can make this code look more expressive by `range`-ing over our custom `countdownFrom` iterator.
656-
657-
To write an iterator, a function that can be used in a `range` loop, you need to write a function in a particular way. From the docs:
657+
To write an iterator like `countDownFrom`, you need to write a function in a particular way. From the docs:
658658

659659
The “range” clause in a “for-range” loop now accepts iterator functions of the following types
660660
func(func() bool)
@@ -677,4 +677,4 @@ func countDownFrom(from int) iter.Seq[int] {
677677
}
678678
```
679679

680-
This is a simple iterator, which will yield the numbers in reverse order - perfect for our usecase.
680+
This is a simple iterator, which will yield numbers in reverse order, starting from, `from` - perfect for our usecase.

0 commit comments

Comments
 (0)