Hello, I’m fried fish.

In the last two weeks, we wrote an article entitled “Can you answer the Go question correctly? More than 80% of people got it wrong…” In the article, some friends put forward the following questions for the title:

What is the meaning of naming the return value of the Go function?

case

Function A is named with a return value, and function B is named without a return value. As follows:

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

func b(a) (func(a).error) {
 return func(a) { 
   print("aaa: done")},nil
}
Copy the code

This is common in daily work. In the past two years, I have heard students who do not know the use of a named return value, and do not know the meaning of a return at the end of a function. What is the use of this?

The official explanation

In the official Effective Go, the Named return value parameters are explicitly defined as Named Result parameters.

The function signature is as follows:

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

With three distinct characteristics:

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

If return is not specified, the declared return variable is returned by default.

What the official definition does is “make code shorter and clearer”. For example, if you name the result of the nextInt function, you know that the first return value is value, and the second return value is nextPos, which is an explicit declaration.

advice

A Tour of Go on the official website makes it clear that Named Result Parameters are only recommended for short functions.

For example:

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

func main(a) {
	fmt.Println(split(17))}Copy the code

They impair readability if used in longer functions. I’ve also seen situations where the code gets more and more complicated because it’s too long to write in order to optimize readability.

conclusion

In fact, named return parameters, compared to Go style, are explicitly named returns.

But it also introduces possible ellipses returned within functions that many novice friends will not understand. Or, as described in the first article, the named return argument is written to become a recursive function.

This feature, recommended at ordinary times to consider the use of short and sharp can be considered, long and much advice or do not increase too much tedious.

This article is constantly updated. You can read it on wechat by searching “Brain into fried fish”. GitHub github.com/eddycjy/blo… Already included, learning Go language can see Go learning map and route, welcome Star urged more.

Recommended reading

  • You are Google, Go: I am not
  • New in Go1.18: Compiled binaries that will contain more information