August 2021
Beginner
112 pages
1h 23m
English
| Puzzle 16 | What’s in a Channel? |
| | package main |
| | |
| | import ( |
| | "fmt" |
| | ) |
| | |
| | func main() { |
| | ch := make(chan int, 2) |
| | ch <- 1 |
| | ch <- 2 |
| | <-ch |
| | close(ch) |
| | a := <-ch |
| | b := <-ch |
| | fmt.Println(a, b) |
| | } |
Guess the Output | |
|---|---|
|
|
Try to guess what the output is before moving to the next page. |
This code will print: 2 0
ch is a buffered channel with a capacity of 2 (you can check this with cap(ch)). You send two values to ch, 1 and 2. If you try ...
Read now
Unlock full access