-
Notifications
You must be signed in to change notification settings - Fork 7
/
chansyn.go
40 lines (38 loc) · 930 Bytes
/
chansyn.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
/*
Example of using an unbuffered channel, based on gocourseday3.pdf.
*/
package main
//
import (
"fmt"
"time"
)
//
// gocourseday3.pdf - simple synchronous channel example.
// Slightly embellished.
//
func main() {
fmt.Println("Start ....")
//
syn_chan := make(chan int) // Synchronous: no buffering specified
done_chan := make(chan bool) // Also synchronous
go func() {
time.Sleep(2 * 1e9)
fmt.Println("receiver is up")
x := <-syn_chan // Receive. Will block until data received
fmt.Println("starting work, received", x)
tw := time.Duration(x * 1e9)
time.Sleep(tw) // Simulate work
fmt.Println("work complete")
done_chan <- true // Signal done
}()
//
fmt.Println("sending", 5)
syn_chan <- 5 // Send. Will block until a receive complete
fmt.Println("sent", 5)
fmt.Println("waiting for complete work")
done_flag := <-done_chan
fmt.Println("done is:", done_flag)
//
fmt.Println("End ....")
}