-
Notifications
You must be signed in to change notification settings - Fork 2
/
channel_example.go
43 lines (32 loc) · 1.01 KB
/
channel_example.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
// channel --> type of queue or sequence, after one func code complete it's send a signal of completion.
// Channels are a typed conduit through which you can send and receive values with the channel operator, <-.
// ch <- v send v to channel ch,
// v := <-ch receive from ch and assign value to v
// data type must be same, goroutine == channel count, keyword chan,
// var variable-name chan type or by shorthand/make func "variable-name := make(chan type)
package main
import "fmt"
// initialization of channel
func multiple(c chan int, value int){
// sending data
c <- value * 10
}
func t1(ch chan int){
fmt.Println(250 + <-ch)
// close channel after process over
// defer close(ch)
}
func main(){
// define channel
codeValue := make(chan int)
// signal sends codeValue to channel
// declare goroutine
go multiple(codeValue, 20)
// receiving data
store := <-codeValue
fmt.Println(store)
// declare goroutine
go t1(codeValue)
// sending data
codeValue <- 50
}