-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoRoutines.go
46 lines (38 loc) · 872 Bytes
/
goRoutines.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
package main
import "fmt"
func fs(s string, c int) {
for i := 0; i < c; i ++ {
fmt.Printf("Printing %s : %d \n", s, i)
}
}
func main() {
fs("hello", 10)
//buffered channel can accepts values without a reciever
ch := make(chan []string, 2)
go fs("go routine", 200)
go func() {
//create an array of string
chStr := make([]string, 100)
chStr1 := make([]string, 100)
chStr2 := make([]string, 100)
for i := 0; i < 100; i ++ {
chStr[i] = "Hello from channel \n"
chStr1[i] = "channel2 \n"
chStr2[i] = "channel3 \n"
}
//put it to the channel has size 2 still can recieve more
ch<-chStr
ch<-chStr1
ch<-chStr2
}()
var input string
//recieving from the channel
fromChan := <-ch
fmt.Println(fromChan)
fromChan2 := <-ch
fmt.Println(fromChan2)
fromChan3 := <-ch
fmt.Println(fromChan3)
fmt.Scanln(&input)
fmt.Println("Done")
}