Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Nowcode 1

Let’s brush up the test this time

  • Remember when you used to go to NowCode when you were preparing for an interview
  • Brush related basic knowledge questions, will go to Leetcode brush algorithm questions
  • About the project, I will ask my friends to interview me for the project

This time, several questions will briefly touch on Golang’s Chan, Fallthrough, closures, etc

I. Title Description:

Topic 1: About Chan

Based on the description of the problem, what should the problem say? What is the question?

  • What is the use of the passage

(struct{}) (struct{}) (struct{}) (struct{}) (struct{}) (struct{})

Output 1, 0? Or will the program panic?

The result is panic

Here’s why:

After initializing the CH channel, a coroutine is immediately opened to close the CH channel. After closing the channel, it immediately writes data to a closed channel. This is obviously panic, which is not allowed in Golang

I have also shared information about channels in my history articles, which can be found in the links to GO channels and Sync packages

Topic 2: About Fallthrough

  • This topic describes the application of FallThrough and switch

Fallthrough and switch are the keywords in Golang. Switch has the same meaning as swith in C language, but the case of switch in Golang is followed by break by default

So at this point, when we expect the program to execute case 1, and then continue to execute case 2, we can use the fallthrough keyword, and one of the things we need to notice about fallthrough is that it forces the following case code, Whether or not the following case is true

So the answer is: 3

Golang switch and fallthrough: Fallthrough is a fallthrough case, but the case must be true to execute it. Fallthrough enforces subsequent cases

Topic three: On closures

What does the following code output?

  • The use of closures

Closures can be thought of this way

In languages that support functions as first-class citizens, the return value of one function is another function, and the returned function can access variables inside the parent function. When the returned function is executed externally, a closure is generated

Here the app function returns another function, so a closure is generated

In Golang, anonymous functions are also closures and can refer to variables defined by the outer function

So here, we need to be aware that the closure is in an environment that can refer to values in that environment

So what’s the result of this case?

答 案 :

Hi All
Hi go go
Hi All All
Copy the code

We see that in the source code, the app function returns another function that can use the variable t of the parent function

The first time you call b, it says Hi All and you understand, the closure refers to the value in the environment

The first time b is called, t is already Hi All, so the next time B is called, the result will be Hi All All

The same thing goes for a

Today is here, learning, if there is a deviation, please correct

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~