-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (38 loc) · 776 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
38
39
40
41
42
43
44
package timeouts
import (
"fmt"
"time"
)
/* Timeouts are important for programs that connect to external resources or that otehrwise
need to bound execution time.
*/
func Run() {
afterTimeout()
beforeTimeout()
}
func afterTimeout() {
channelOne := make(chan string, 1)
go func() {
time.Sleep(2 * time.Second)
channelOne <- "SentToOne"
}()
select {
case result := <-channelOne:
fmt.Println(result)
case <-time.After(1 * time.Second):
fmt.Println("Timeout 1!")
}
}
func beforeTimeout() {
channelTwo := make(chan int, 1)
go func() {
time.Sleep(1 * time.Second)
channelTwo <- 1337
}()
select {
case result := <-channelTwo:
fmt.Println(result)
case <-time.After(22 * time.Second):
fmt.Println("We got a result before, this never runs.")
}
}