SoFunction
Updated on 2025-03-02

golang return omit usage description

If the return value defines a variable, the return must be followed by the expression or value.

func main() {
 ("-------", test())
}
func test() (n string) {
 n = "hello"
 return
}

If no variable is defined, return must be displayed to return the object

func main() {
 ("-------", test())
}
func test() string {
 n := "hello"
 return n
}

Supplement: The pitfall in the execution order between defer, return and return values ​​in Golang

In Go language, the delay function defer serves as the important task of cry...catch and is very easy to use. However, in actual applications, many gophers do not really understand the execution order between defer, return and return value, and thus fall into the pit. Today we will unveil its mystery!

Let's run the following two pieces of code:

A. The case of an unnamed return value

package main 
import (
 "fmt"
)
 
func main() {
 ("return:", a()) // Print result is return: 0}
 
func a() int {
 var i int
 defer func() {
 i++
 ("defer2:", i) // Print result is defer: 2 }()
 defer func() {
 i++
 ("defer1:", i) // Print result is defer: 1 }()
 return i
}<br><br>

B. Case of famous return value

package main
 
import (
 "fmt"
)
 
func main() {
 ("return:", b()) // Print result is return: 2}
 
func b() (i int) {
 defer func() {
 i++
 ("defer2:", i) // Print result is defer: 2 }()
 defer func() {
 i++
 ("defer1:", i) // Print result is defer: 1 }()
 return i // Or directly return the same effect}

Let’s first assume a conclusion to help you understand the reasons:

1. The execution order of multiple defers is "last in first out";

2. The execution logic of defer, return and return should be: return is executed first, return is responsible for writing the result to the return value; then defer starts to perform some final work; finally the function exits with the current return value.

How to explain the difference between the two results:

The reason why the return results of the above two codes are different is that it is easy to understand from the conclusion above.

The return value of the a()int function is not pre-known, and its value comes from the assignment of other variables. The modified in defer is also other variables, not the return value itself. Therefore, the return value is not changed when the function exits.

The return value of the b()(i int) function is known in advance, which means that the real return value can be called in defer. Therefore, after the return value is assigned to the return value i, defer modifies the value of i again. The final return value after the function exits will be the value modified by defer.

C. Let’s look at the third example below to verify the above conclusion:

package main 
import (
 "fmt"
)
 
func main() {
 ("c return:", *(c())) // Print result is c return: 2}
 
func c() *int {
 var i int
 defer func() {
 i++
 ("c defer2:", i) // Print result is c defer: 2 }()
 defer func() {
 i++
 ("c defer1:", i) // Print result is c defer: 1 }()
 return &i
}

Although the return value of c()*int is not declared in advance, since the return value of c()*int is a pointer variable, after returning assigning the address of variable i to the return value, defer modifies the actual value of i in memory again. Therefore, although the return value when the function exits is still the original pointer address, the actual value of memory it points to has been successfully modified.

That is, the conclusion we assume is correct!

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.