1

Hello everyone, I am fried fish.

In the first two weeks of our article " Can you answer this Go question correctly?" More than 80% of the people answered incorrectly... "In the article, a small partner asked the following questions for the title:

To this end, today we will take a look at what is the meaning of the return value naming of the Go function?

case

Function a with a return value, function b is an example named without a return value. as follows:

func a() (done func(), err error) {
 return func() { 
   print("aaa: done") 
 }, nil
}

func b() (func(), error) {
 return func() { 
   print("aaa: done") 
 }, nil
}

This is the most common in daily work. In the past two years, I heard that some students did not know the usage of named return values, and did not know the meaning of returning return at the end of the function. What is the use?

official explanation

In the official "Effective Go", the named return value parameters are clearly defined as Named result parameters (named result parameters).

The function signature is as follows:

func nextInt(b []byte, pos int) (value, nextPos int) {

with three distinct characteristics:

  1. The return or result "parameters" of a Go function can be named and used as regular variables, just like passed parameters.
  2. When named, they are initialized to the zero value of their type at the start of the function.
  3. If the function executes a return statement with no parameters, the current value of the result parameter is used as the return value.

To put it simply, it is the same as the regular input parameter. It will have a zero value after the declaration. If return not specified, the declared return variable will be returned by default.

The official definition of the role is: "Can make the code shorter and ". Like naming the result returned by the nextInt function, then you can clearly know that the first returned value is value , and the second is nextPos , which plays the role of explicitly declaring .

suggestion

In "A Tour of Go" on the official website, it is clearly pointed out that Named result parameters are only recommended for use in shorter functions.

For example the following example:

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return
}

func main() {
    fmt.Println(split(17))
}

They hurt readability if used in longer functions. I have also seen the scene where the code is written with continuous line breaks in order to optimize readability because it is too long.

Summarize

Actually named return parameters, more Go-style, are to explicitly name the return.

But it will also bring about the possible omission of the return in the function, so that many people who are new to the game can't understand it. Or as described in the opening article, the named return parameter is written to become a recursive function, and a tremor will also appear.

This feature is recommended to be used at a discretionary level. It can be considered if it is short and shrewd. It is recommended not to add too much cumbersomeness to the long and large one.

The article is continuously updated, you can read it on WeChat search [Brain into the fried fish], this article GitHub github.com/eddycjy/blog has been included, learn Go language, you can see Go learning map and route

Recommended reading


煎鱼
8.4k 声望12.8k 粉丝