~kotrunga/a-tour-of-go

a-tour-of-go/concurrency/3.6_default-selection.go -rw-r--r-- 520 bytes View raw
b3f98925kotrunga remove .DS_Store file 8 months ago
                                                                                
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
/*
	Notes 3.6

	You can add a default case to your select which
	will run if no other case is ready.

	select {
	case i := <-c:
		// use i
	default:
		// receiving from c would block
	}
*/

package main

import (
	"fmt"
	"time"
)

func main() {
	tick := time.Tick(100 * time.Millisecond)
	boom := time.After(500 * time.Millisecond)
	for {
		select {
		case <- tick:
			fmt.Println("tick.")
		case <- boom:
			fmt.Println("BOOM!")
			return
		default:
			fmt.Println("    .")
			time.Sleep(50 * time.Millisecond)
		}
	}
}