August 2021
Beginner
112 pages
1h 23m
English
| Puzzle 8 | Sleep Sort |
| | package main |
| | |
| | import ( |
| | "fmt" |
| | "sync" |
| | "time" |
| | ) |
| | |
| | func main() { |
| | var wg sync.WaitGroup |
| | for _, n := range []int{3, 1, 2} { |
| | wg.Add(1) |
| | go func() { |
| | defer wg.Done() |
| | time.Sleep(time.Duration(n) * time.Millisecond) |
| | fmt.Printf("%d ", n) |
| | }() |
| | } |
| | wg.Wait() |
| | fmt.Println() |
| | } |
Guess the Output | |
|---|---|
|
|
Try to guess what the output is before moving to the next page. |
This code will print: 2 2 2
You probably expected 1 2 3. Each goroutine sleeps n milliseconds and then prints n.
Read now
Unlock full access