interview questions
This is the second article about channels in the Go Quiz series, covering the characteristics of channel
select
and the channel
when using 061f60641c2da8 and 061f60641c2da9 together.
This question comes from Valentin Deleplace, an engineer at Google.
package main
import "fmt"
func main() {
data := make(chan int)
shutdown := make(chan int)
close(shutdown)
close(data)
select {
case <-shutdown:
fmt.Print("CLOSED, ")
case data <- 1:
fmt.Print("HAS WRITTEN, ")
default:
fmt.Print("DEFAULT, ")
}
}
- A: Enter the default branch and print "DEFAULT, "
- B: Enter the shutdown branch and print "CLOSED, "
- C: Enter the data branch and print "HAS WRITTEN, "
- D: The program will panic
- E: The program may panic, or it may print "CLOSED, "
This question mainly examines the following knowledge points:
channel
is closed, what will be the result of receiving datachannel
channel
select
the operating mechanism of 061f60641c2ee2?
Parse
- For
channel
, sending datachannel
channel
will block. For
nil channel
and 061f60641c2fb6 with bufferchannel
and receiving data is shown in the following table:channel nil empty Not empty not full full send data to channel block Sent successfully Sent successfully block Receive data from channel block block Received successfully Received successfully close channel panic Closed successfully Closed successfully Closed successfully channel
is closed:- Sending data to the closed
channel
will trigger a panic. Closed from
channel
receive data, it will first readchannel
in the data. If the data is read, continuing to read datachannel
will get the zero value of the element type stored inchannel
data, ok := <- c
For the above codes, if Channel
c
closed, continued fromc
read data in, whenc
when there are still data,data
is the corresponding value read,ok
value istrue
. Ifc
data has been read, and thatdata
is zero value,ok
value isfalse
.channel
is closed, it will panic if it is closed again.
- Sending data to the closed
The operating mechanism of
select
- Select an executable non-blocking
case
branch. If multiplecase
branches are not blocked, acase
branch will be randomly selected for execution, andcase
branch is written in the code does not matter. - If all
case
branches are blocked, it will enter thedefault
branch for execution. - If there is no
default
branch, thenselect
will block until there is acase
branch that does not block.
- Select an executable non-blocking
According to the above rules, the title at the beginning of this article, when running
- Both the data and shutdown channels are closed.
- For a closed channel, the data received from the channel is the zero value of the element type stored in the channel, so
case <-shutdown
will not block. - For a closed channel, sending data to it will cause a panic, so
case data <- 1
will not block and will cause a panic. - Therefore, when the select statement is executed, both case branches will not block and may be executed. If
case <-shutdown
is executed, "CLOSED, " will be printed. Ifcase data <- 1
is executed, it will cause the program to panic.
So the answer to this question is E
.
snack
You can review the first topic about channels in the Go quiz series to deepen your understanding of channels.
Topic link address: channel Interview questions and precautions
open source address
The open source address of the article and sample code is on GitHub: https://github.com/jincheng9/go-tutorial
Official account: coding advanced
Personal website: https://jincheng9.github.io/
Know: https://www.zhihu.com/people/thucuhkwuji
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。