1
Private & Confidential
Channel in Go
Jaych Su - 3 Feb 2021
2
Private & Confidential 2
What is Channel
”
A channel provides a mechanism for
concurrently executing functions to
communicate by sending and receiving values
of a specified element type.
-- Golang Spec (//golang.org/ref/spec#Channel_types)
3
Private & Confidential 3
What behind Channel
QWhat will happen if read from/write to
a closed channel?
4
Private & Confidential 4
<- Closed Channel <-
What behind Channel
x, ok := <- chan
- [unread data] x is remaining value, ok is true
- [has been read] x is falsy value, ok is false
- [empty] x is falsy value, ok is false
5
Private & Confidential 5
<- Closed Channel <-
What behind Channel
panic
x, ok := <- chan
- [unread data] x is remaining value, ok is true
- [has been read] x is falsy value, ok is false
- [empty] x is falsy value, ok is false
chan <- x
6
Private & Confidential 6
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
7
Private & Confidential 7
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
8
Private & Confidential 8
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
buf (doubly circular linked list)
lock
11
12
13
14
goroutine
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
9
Private & Confidential 9
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
buf (doubly circular linked list)
lock
goroutine
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
11
12
13
14
10
Private & Confidential 10
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
12
13
14
11
Private & Confidential 11
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
12
13
14
12
Private & Confidential 12
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
12
13
14
13
Private & Confidential 13
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
13
14
14
Private & Confidential 14
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
13
14
15
Private & Confidential 15
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
13
14
16
Private & Confidential 16
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
17
Private & Confidential 17
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
18
Private & Confidential 18
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
19
Private & Confidential 19
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c
close(c)
<- c
<- c
c <- 14
12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
20
Private & Confidential 20
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c
close(c)
<- c
<- c
c <- 14
12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
21
Private & Confidential 21
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c
close(c)
<- c
<- c
c <- 14
12
13
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
22
Private & Confidential 22
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c
<- c
c <- 14
13
lock
goroutine buf (doubly circular linked list)
sendx recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
23
Private & Confidential 23
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c
<- c
c <- 14
13
goroutine buf (doubly circular linked list)
sendx recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
24
Private & Confidential 24
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c // 13
<- c
c <- 14
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
13
25
Private & Confidential 25
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c // 13
<- c // 0
c <- 14
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
13
26
Private & Confidential 26
What behind Channel
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
panic: send on closed channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
27
Private & Confidential 27
chan <- x
<- Closed Channel <-
What behind Channel
panic
x, ok := <- chan
- [unread data] x is remaining value, ok is true
- [has been read] x is falsy value, ok is false
- [empty] x is falsy value, ok is false
28
Private & Confidential 28
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
29
Private & Confidential 29
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
30
Private & Confidential 30
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
31
Private & Confidential 31
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
32
Private & Confidential 32
What crashes Channel
QWhat crashes Channel?
33
Private & Confidential 33
What crashes Channel
QWhat crashes Channel?
What if buf is full?
34
Private & Confidential 34
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
sendx
recvx
11 12
14 13
35
Private & Confidential 35
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
sendx
recvx
11 12
14 13
sendq
recvq
36
Private & Confidential 36
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
37
Private & Confidential 37
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
38
Private & Confidential 38
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
39
Private & Confidential 39
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
<- c
goroutine
40
Private & Confidential 40
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
<- c
goroutine
41
Private & Confidential 41
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
recvx
sendx
12
14 13
sendq
recvq
c <- 15
goroutine
<- c
// 11
goroutine
42
Private & Confidential 42
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
<- c
// 11
goroutine
lock
recvx
sendx
43
Private & Confidential 43
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
lock
recvx
sendx
44
Private & Confidential 44
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
lock
recvx
sendx
45
Private & Confidential 45
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
lock
recvx
sendx
46
Private & Confidential 46
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
sendx
recvx
15 12
14 13
sendq
recvq
goroutine
lock
47
Private & Confidential 47
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
15 12
14 13
sendq
recvq
goroutine
lock
sendx
recvx
48
Private & Confidential 48
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
15 12
14 13
sendq
recvq
lock
sendx
recvx
49
Private & Confidential 49
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
50
Private & Confidential 50
func main() {
var c chan int
go func() {
c <- 1
}()
<-c
}
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
51
Private & Confidential 51
for {
select {
case x, ok := <-c1:
...
case x, ok := <-c2:
...
default:
...
}
}
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
52
Private & Confidential 52
select {
case <- c:
// get from channel c
case <- time.After(2 * time.Second)
// timeout when getting from channel c
}
When uses Channel
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
53
Private & Confidential 53
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
func consumer(c chan int) {
for x := range c {
...
}
}
func producer(c chan int) {
for _, x := range values {
c <- x
}
}
54
Private & Confidential 54
// usually used for declaration
func foo(ch chan<- int) <-chan int {
...
}
When uses Channel
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
55
Private & Confidential 55
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
[PLUS] Something interesting
56
Private & Confidential 56
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
57
Private & Confidential 57
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
58
Private & Confidential 58
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
D
59
Private & Confidential 59
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
1
D
60
Private & Confidential 60
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
2
1
D
61
Private & Confidential 61
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
62
Private & Confidential 62
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
63
Private & Confidential 63
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
64
Private & Confidential 64
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
65
Private & Confidential 65
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
66
Private & Confidential 66
Thank you!

More Related Content

PDF
Golang Channels
PPTX
Stressen's matrix multiplication
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
PPTX
Greedy algorithms
PPT
Recurrences
PPT
3.9 external sorting
PDF
A greedy algorithms
PPT
Late and Early binding in c++
Golang Channels
Stressen's matrix multiplication
Webinar : P, NP, NP-Hard , NP - Complete problems
Greedy algorithms
Recurrences
3.9 external sorting
A greedy algorithms
Late and Early binding in c++

What's hot (20)

PDF
DBMS 2 | Entity Relationship Model
PPTX
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
PPTX
Code Optimization
PPT
Parsing
PPTX
File in C language
PPTX
Huffman coding
PPT
Structure of C++ - R.D.Sivakumar
PPT
Lec 17 heap data structure
PPTX
Merging files (Data Structure)
PPTX
Log based and Recovery with concurrent transaction
PPTX
PPTX
Bash shell scripting
PPTX
C programming language tutorial
PPT
Graphics Lecture 7
PDF
Keywords, identifiers ,datatypes in C++
PPTX
Pumping lemma for regular set h1
PPTX
LR(1) and SLR(1) parsing
PPTX
Universal turing coastus
PPTX
Job sequencing with Deadlines
PPTX
Demultiplexer presentation
DBMS 2 | Entity Relationship Model
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Code Optimization
Parsing
File in C language
Huffman coding
Structure of C++ - R.D.Sivakumar
Lec 17 heap data structure
Merging files (Data Structure)
Log based and Recovery with concurrent transaction
Bash shell scripting
C programming language tutorial
Graphics Lecture 7
Keywords, identifiers ,datatypes in C++
Pumping lemma for regular set h1
LR(1) and SLR(1) parsing
Universal turing coastus
Job sequencing with Deadlines
Demultiplexer presentation
Ad

Similar to Channel in Go (20)

PDF
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
PDF
Sockets
PDF
scala-gopher: async implementation of CSP for scala
PDF
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverter
PPTX
03 - Refresher on buffer overflow in the old days
PPTX
Basics of sockets
PDF
OpenSSL Basic Function Call Flow
PDF
PPTX
Using Git as your VCS with Bioconductor
PDF
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
PDF
Querying 1.8 billion reddit comments with python
PDF
From Zero to Application Delivery with NixOS
PDF
HOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOP
PPTX
Docker Networking with New Ipvlan and Macvlan Drivers
PDF
Netty from the trenches
PDF
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
PDF
Networking and Go: An Epic Journey
PDF
Os lab final
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Sockets
scala-gopher: async implementation of CSP for scala
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverter
03 - Refresher on buffer overflow in the old days
Basics of sockets
OpenSSL Basic Function Call Flow
Using Git as your VCS with Bioconductor
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
Querying 1.8 billion reddit comments with python
From Zero to Application Delivery with NixOS
HOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOP
Docker Networking with New Ipvlan and Macvlan Drivers
Netty from the trenches
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Networking and Go: An Epic Journey
Os lab final
Ad

Recently uploaded (20)

PDF
The influence of sentiment analysis in enhancing early warning system model f...
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
STKI Israel Market Study 2025 version august
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PPTX
Modernising the Digital Integration Hub
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PPTX
Configure Apache Mutual Authentication
PPT
What is a Computer? Input Devices /output devices
The influence of sentiment analysis in enhancing early warning system model f...
Microsoft Excel 365/2024 Beginner's training
sustainability-14-14877-v2.pddhzftheheeeee
A contest of sentiment analysis: k-nearest neighbor versus neural network
1 - Historical Antecedents, Social Consideration.pdf
sbt 2.0: go big (Scala Days 2025 edition)
OpenACC and Open Hackathons Monthly Highlights July 2025
Getting started with AI Agents and Multi-Agent Systems
STKI Israel Market Study 2025 version august
Custom Battery Pack Design Considerations for Performance and Safety
Developing a website for English-speaking practice to English as a foreign la...
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Benefits of Physical activity for teenagers.pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
A review of recent deep learning applications in wood surface defect identifi...
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Modernising the Digital Integration Hub
Convolutional neural network based encoder-decoder for efficient real-time ob...
Configure Apache Mutual Authentication
What is a Computer? Input Devices /output devices

Channel in Go

  • 1. 1 Private & Confidential Channel in Go Jaych Su - 3 Feb 2021
  • 2. 2 Private & Confidential 2 What is Channel ” A channel provides a mechanism for concurrently executing functions to communicate by sending and receiving values of a specified element type. -- Golang Spec (//golang.org/ref/spec#Channel_types)
  • 3. 3 Private & Confidential 3 What behind Channel QWhat will happen if read from/write to a closed channel?
  • 4. 4 Private & Confidential 4 <- Closed Channel <- What behind Channel x, ok := <- chan - [unread data] x is remaining value, ok is true - [has been read] x is falsy value, ok is false - [empty] x is falsy value, ok is false
  • 5. 5 Private & Confidential 5 <- Closed Channel <- What behind Channel panic x, ok := <- chan - [unread data] x is remaining value, ok is true - [has been read] x is falsy value, ok is false - [empty] x is falsy value, ok is false chan <- x
  • 6. 6 Private & Confidential 6 type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
  • 7. 7 Private & Confidential 7 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... }
  • 8. 8 Private & Confidential 8 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 buf (doubly circular linked list) lock 11 12 13 14 goroutine sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... }
  • 9. 9 Private & Confidential 9 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 buf (doubly circular linked list) lock goroutine sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 11 12 13 14
  • 10. 10 Private & Confidential 10 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 12 13 14
  • 11. 11 Private & Confidential 11 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 12 13 14
  • 12. 12 Private & Confidential 12 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 12 13 14
  • 13. 13 Private & Confidential 13 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 13 14
  • 14. 14 Private & Confidential 14 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 13 14
  • 15. 15 Private & Confidential 15 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 13 14
  • 16. 16 Private & Confidential 16 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14
  • 17. 17 Private & Confidential 17 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14
  • 18. 18 Private & Confidential 18 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14
  • 19. 19 Private & Confidential 19 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c close(c) <- c <- c c <- 14 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11
  • 20. 20 Private & Confidential 20 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c close(c) <- c <- c c <- 14 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11
  • 21. 21 Private & Confidential 21 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c close(c) <- c <- c c <- 14 12 13 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11
  • 22. 22 Private & Confidential 22 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c <- c c <- 14 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12
  • 23. 23 Private & Confidential 23 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c <- c c <- 14 13 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12
  • 24. 24 Private & Confidential 24 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c // 13 <- c c <- 14 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12 13
  • 25. 25 Private & Confidential 25 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c // 13 <- c // 0 c <- 14 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12 13
  • 26. 26 Private & Confidential 26 What behind Channel c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 panic: send on closed channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... }
  • 27. 27 Private & Confidential 27 chan <- x <- Closed Channel <- What behind Channel panic x, ok := <- chan - [unread data] x is remaining value, ok is true - [has been read] x is falsy value, ok is false - [empty] x is falsy value, ok is false
  • 28. 28 Private & Confidential 28 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 29. 29 Private & Confidential 29 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 30. 30 Private & Confidential 30 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 31. 31 Private & Confidential 31 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 32. 32 Private & Confidential 32 What crashes Channel QWhat crashes Channel?
  • 33. 33 Private & Confidential 33 What crashes Channel QWhat crashes Channel? What if buf is full?
  • 34. 34 Private & Confidential 34 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock sendx recvx 11 12 14 13
  • 35. 35 Private & Confidential 35 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock sendx recvx 11 12 14 13 sendq recvq
  • 36. 36 Private & Confidential 36 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq
  • 37. 37 Private & Confidential 37 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine
  • 38. 38 Private & Confidential 38 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine
  • 39. 39 Private & Confidential 39 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine <- c goroutine
  • 40. 40 Private & Confidential 40 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine <- c goroutine
  • 41. 41 Private & Confidential 41 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling recvx sendx 12 14 13 sendq recvq c <- 15 goroutine <- c // 11 goroutine
  • 42. 42 Private & Confidential 42 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine <- c // 11 goroutine lock recvx sendx
  • 43. 43 Private & Confidential 43 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine lock recvx sendx
  • 44. 44 Private & Confidential 44 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine lock recvx sendx
  • 45. 45 Private & Confidential 45 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine lock recvx sendx
  • 46. 46 Private & Confidential 46 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling sendx recvx 15 12 14 13 sendq recvq goroutine lock
  • 47. 47 Private & Confidential 47 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 15 12 14 13 sendq recvq goroutine lock sendx recvx
  • 48. 48 Private & Confidential 48 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 15 12 14 13 sendq recvq lock sendx recvx
  • 49. 49 Private & Confidential 49 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel
  • 50. 50 Private & Confidential 50 func main() { var c chan int go func() { c <- 1 }() <-c } 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel
  • 51. 51 Private & Confidential 51 for { select { case x, ok := <-c1: ... case x, ok := <-c2: ... default: ... } } 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel
  • 52. 52 Private & Confidential 52 select { case <- c: // get from channel c case <- time.After(2 * time.Second) // timeout when getting from channel c } When uses Channel 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel
  • 53. 53 Private & Confidential 53 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel func consumer(c chan int) { for x := range c { ... } } func producer(c chan int) { for _, x := range values { c <- x } }
  • 54. 54 Private & Confidential 54 // usually used for declaration func foo(ch chan<- int) <-chan int { ... } When uses Channel 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel
  • 55. 55 Private & Confidential 55 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] [PLUS] Something interesting
  • 56. 56 Private & Confidential 56 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting
  • 57. 57 Private & Confidential 57 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list
  • 58. 58 Private & Confidential 58 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list D
  • 59. 59 Private & Confidential 59 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list 1 D
  • 60. 60 Private & Confidential 60 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list 2 1 D
  • 61. 61 Private & Confidential 61 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 62. 62 Private & Confidential 62 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 63. 63 Private & Confidential 63 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 64. 64 Private & Confidential 64 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 65. 65 Private & Confidential 65 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 66. 66 Private & Confidential 66 Thank you!