example of using range/close/for with channels
This commit is contained in:
parent
3b54caef82
commit
a8b0538b1b
22
concurrency/range-close/go.go
Normal file
22
concurrency/range-close/go.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func fibonacci(n int, c chan uint64) {
|
||||
var x, y uint64 = 0, 1
|
||||
for i := 0; i < n; i++ {
|
||||
c <- x
|
||||
x, y = y, x+y
|
||||
}
|
||||
close(c)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fc := make(chan uint64, 80)
|
||||
go fibonacci(cap(fc), fc)
|
||||
for i := range fc {
|
||||
fmt.Println(i)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user