August 2021
Beginner
112 pages
1h 23m
English
| Puzzle 13 | Free-Range Integers |
| | package main |
| | |
| | import ( |
| | "fmt" |
| | ) |
| | |
| | func fibs(n int) chan int { |
| | ch := make(chan int) |
| | |
| | go func() { |
| | a, b := 1, 1 |
| | for i := 0; i < n; i++ { |
| | ch <- a |
| | a, b = b, a+b |
| | } |
| | }() |
| | |
| | return ch |
| | } |
| | |
| | func main() { |
| | for i := range fibs(5) { |
| | fmt.Printf("%d ", i) |
| | } |
| | fmt.Println() |
| | } |
Guess the Output | |
|---|---|
|
|
Try to guess what the output is before moving to the next page. |
This code will deadlock.
To get a value from a channel, you can either use the receive operator (←ch) or ...
Read now
Unlock full access