头图

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 data channel channel
  • select the operating mechanism of 061f60641c2ee2?

Parse

  1. For channel , sending data channel channel will block.
  2. For nil channel and 061f60641c2fb6 with buffer channel and receiving data is shown in the following table:

    channelnilemptyNot empty not fullfull
    send data to channelblockSent successfullySent successfullyblock
    Receive data from channelblockblockReceived successfullyReceived successfully
    close channelpanicClosed successfullyClosed successfullyClosed successfully
  3. channel is closed:

    • Sending data to the closed channel will trigger a panic.
    • Closed from channel receive data, it will first read channel in the data. If the data is read, continuing to read data channel will get the zero value of the element type stored in channel

      data, ok := <- c 

      For the above codes, if Channel c closed, continued from c read data in, when c when there are still data, data is the corresponding value read, ok value is true . If c data has been read, and that data is zero value, ok value is false .

    • channel is closed, it will panic if it is closed again.
  4. The operating mechanism of select

    • Select an executable non-blocking case branch. If multiple case branches are not blocked, a case branch will be randomly selected for execution, and case branch is written in the code does not matter.
    • If all case branches are blocked, it will enter the default branch for execution.
    • If there is no default branch, then select will block until there is a case branch that does not block.

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. If case 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

References


coding进阶
121 声望18 粉丝