-
Notifications
You must be signed in to change notification settings - Fork 86
/
channels.go
73 lines (62 loc) · 1.83 KB
/
channels.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"time"
)
// Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.
// Do not communicate by sharing memory; instead, share memory by communicating.
// SomeComputation When done send true through a channel received as parameter
func SomeComputation(done chan bool) {
fmt.Print("working...")
// 1 second sleep
time.Sleep(time.Second)
fmt.Println("done")
// send done and close channel
done <- true
}
// Hello receive some string through input channel, concatenate with "Hello " and returns
// a new string through output channel
// input is specified as read-only channel and output as write-only
func Hello(input <-chan string, output chan<- string) {
received := <-input
received = "Hello " + received
output <- received
}
// Echo returns what receive through two way channel
func Echo(inout chan string) {
inout <- <-inout
}
func main() {
// syntax note
// <- channel receive from channel
// channel <- send to channel
// sending some message back from a goroutine
message := make(chan string)
go func(m chan string) {
m <- "some message"
}(message)
fmt.Println(<-message)
// buffered channels are like convetional, but can store
// more values until you request the next
msgs := make(chan string, 2)
msgs <- "buffered"
msgs <- "channel"
fmt.Println(<-msgs)
fmt.Println(<-msgs)
done := make(chan bool)
go SomeComputation(done)
// wait until some computation are done
<-done
// send from "in" channel and receive from "out"
in := make(chan string)
out := make(chan string)
go Hello(in, out)
in <- "John"
fmt.Println(<-out)
// for default channels are two way
// we can send and receive data
echoChan := make(chan string)
go Echo(echoChan)
echoChan <- "Ping"
fmt.Println(<-echoChan)
}