-
Notifications
You must be signed in to change notification settings - Fork 238
/
main.go
38 lines (30 loc) · 1.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"fmt"
"math/rand"
"time"
)
func boring(msg string) {
for i := 0; ; i++ {
fmt.Println(msg, i)
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
}
}
func main() {
// after run this line, the main goroutine is finished.
// main goroutine is a caller. It doesn't wait for func boring finished
// Thus, we don't see anything
go boring("boring!") // spawn a goroutine. (1)
// To solve it, we can make the main go routine run forever by `for {}` statement.
// for {
// }
// A little more interesting is the main goroutine exit. the program also exited
// This code hang
fmt.Println("I'm listening")
time.Sleep(2 * time.Second)
fmt.Println("You're boring. I'm leaving")
// However, the main goroutine and boring goroutine does not communicate each other.
// Thus, the above code is cheated because the boring goroutine prints to stdout by its own function.
// the line `boring! 1` that we see on terminal is the output from boring goroutine.
// real conversation requires a communication
}