-
Notifications
You must be signed in to change notification settings - Fork 238
/
main.go
37 lines (33 loc) · 772 Bytes
/
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
package main
import (
"fmt"
"math/rand"
"time"
)
// the boring function return a channel to communicate with it.
func boring(msg string, quit chan string) <-chan string { // <-chan string means receives-only channel of string.
c := make(chan string)
go func() { // we launch goroutine inside a function.
for i := 0; ; i++ {
select {
case c <- fmt.Sprintf("%s %d", msg, i):
// do nothing
case <-quit:
fmt.Println("clean up")
quit <- "See you!"
return
}
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
}
}()
return c // return a channel to caller
}
func main() {
quit := make(chan string)
c := boring("Joe", quit)
for i := 3; i >= 0; i-- {
fmt.Println(<-c)
}
quit <- "Bye"
fmt.Println("Joe say:", <-quit)
}