Channels Flashcards

1
Q

Into what a CPU is splitted?

A

CPU is split into CORES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which keyword is used to spawn a new goroutine?

A

go “name of the function”
e.g.
go doSomething()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are channels?

A

Channels are a typed, thread-safe queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What the job of channels?

A

Channels allow different goroutines to communicate with each other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to create a channel without a buffer?

A

ch := make(chan int)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to send data to a channel?

A

Use the arrow <- notation
ch <- 69

This operation will block until another goroutine is ready to receive the value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to receive data from a channel?

A

v := <-ch

This reads and removes a value from the channel and saves it into the variable v.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a deadlock?

A

A deadlock is when a group of goroutines are all blocking so none of them can continue.

TIP: This is a common bug that you need to watch out for in concurrent programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to create a channel with a buffer?

A

ch := make(chan int, 100)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens on a buffered channel?

A
  1. Sending blocks when the buffer is full
  2. Receiving blocks only when the buffer is empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to close a channel?

A

close(ch)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to check if a channel is closed?

A

v, ok := <-ch

ok is false if the channel is empty and closed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What will happen if we send data to a closed channel?

A

PANIC

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can we leave the channels open?

A

YES!

There’s nothing wrong with leaving channels open, they’ll still be garbage collected if they’re unused.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly