-
Notifications
You must be signed in to change notification settings - Fork 0
/
18_rangeAndClose.go
35 lines (27 loc) · 954 Bytes
/
18_rangeAndClose.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
package main
import "fmt"
// A sender can close a channel to indicate that no more values will be sent.
// Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression
// v, ok <- ch
// if ok if false, there are no more values to receive and the channel is closed
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, 10)
go fibonacci(cap(c), c)
// The loop receives values from the channel repeatedly until it is closed
for i := range c {
fmt.Println(i)
}
}
// NOTE: only sender should close a channel, never the receiver.
// Sending on a closed channel will cause a panic.
// Another NOTE: Channels aren't like files. You don't usually need to close them.
// Closing them is only necessary when the receiver must be told there are no more values comming
// such as to terminate a range loop