28 lines
1.5 KiB
Markdown
28 lines
1.5 KiB
Markdown
# Interview Questions
|
|
|
|
|
|
This doc is not a buntch of real questions from interview. It is more about the different concept in golang programming that could be asked by interviewers.
|
|
|
|
## 1. What is goroutines?
|
|
|
|
Goroutine, for me, I understand it as a detached process. It can run independently of the function that started it.
|
|
|
|
## 2. What is channels?
|
|
|
|
A channel is a pipeline for sending and receiving data. It is like a socket that runs inside your program. It provides a way for one goroutine to send structured data to another.
|
|
|
|
## 3. What is wait group?
|
|
|
|
|
|
|
|
## 4. Race condition
|
|
In a rece condition, two things are "racing" to use the same piece of data. Problems arise when both are working with the same data at around the same time. One goroutine may be only partway through modifying a value when another goroutine tries to use it. And that situation can have unintended consequences.
|
|
|
|
# When to use Channel, when to use Wait Group?
|
|
|
|
I agree with [this answer](https://stackoverflow.com/a/50166588/3409400). More specifically, I summarize my understanding below
|
|
## Channel
|
|
1. If each job's result needs to be collected, using channel is better. For example, we need to know each goroutine's error result. The error can be included in the channel struct while the signal is sent back.
|
|
2. If we want to control the number of goroutine, channel might be better since we can use channel buffer.
|
|
## Wait group
|
|
1. Wait group is easier to use and without controlling the goroutine's number |