go-learning/tour_of_go/l7/range-and-close.go

24 lines
308 B
Go
Raw Normal View History

2025-09-26 14:43:54 +00:00
package main
import (
"fmt"
)
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)
fmt.Printf("The channel's capacity is %d", cap(c))
go fibonacci(cap(c), c)
for i := range c {
fmt.Println(i)
}
}