-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_RoutinesChannels.go
49 lines (42 loc) · 1.23 KB
/
8_RoutinesChannels.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
package main
import (
"fmt"
"runtime"
)
/*
- lightweight threads, no id of go routines
- not constant time of execution
- not always paralelism - scheduler, cores, processors
- m:n planning -> m go routine on n threads of operation system
- GOMAXPROCS == m parameter in m:n planning
- executor tracing
- main go routine terminates child goroutines
- channnels = sync and data flow beetween goroutines
- channels (FIFO), channel has pointer under the hood, nil,
work with any data type
- channel types: bideractional, singledirectional,
bufferized [cap/len functions] or not, open/closed
- channel operations:
create: ch = make (chan int) || ch = make(chan int), 3)
send to channel: ch <- x
receive from channel: x = <-ch || x,ok (channel opened or closed) = <-ch
close: close(ch), will return nil/0 on read from close channel,
write to closed channel triggers panic, who creates -> closes
- select
- timer
- ticker == cron prog
- interrupt <-interrupt => gracefull shutdown
*/
func main() {
go fmt.Println("Channels")
/* quantity of goroutines */
fmt.Println("Goroutines", runtime.NumGoroutine())
/* not bufferized channels */
var ch = make(chan int)
go func() {
fmt.Printf("Hello\n")
ch <- 1
}()
/* blocked until received */
<-ch
}