Functions can have 0 or more return values. The return value needs to specify the data type, and the return value is specified through the return keyword.
- Return can have parameters or no parameters. These return values can have names or no names. Functions in go can have multiple return values.
- When a parameter is specified in the return keyword, the return value may not be named. If return omits the parameter, the return value part must have a name
- When the return value has a name, it must be surrounded by brackets and separated by commas, even if there is only one return value
- But even if the return value is named, the name of other return value can be forced to be specified in return, which means that the priority of return is higher
- The named return value is pre-declared and can be used directly inside the function without declaring it again. The name of the named return value cannot be the same as the function parameter name, otherwise the error prompts the variable to be repeatedly defined.
- There can be expressions in return, but assignment expressions cannot appear, which may be different from other languages. For example, return a+b is correct, but return c=a+b is wrong.
Go language function returns value instance
No return value
func f1() { ("I didn't return the value, just do some calculations") }
There is a return value
func sum(a int, b int) (ret int) { ret = a + b return ret }
Multiple return values, and the returned content is specified in the return
func f2() (name string, age int) { name = "Old Guo" age = 30 return name, age }
Multiple return values, the return value name is not used
func f3() (name string, age int) { name = "Old Guo" age = 30 return // Equivalent to return name, age}
return override the name of the return value, the return value name is not used
func f4() (name string, age int) { n := "Old Guo" a := 30 return n, a }
In Go, one of the return values is often used as a condition to determine whether the function is executed successfully and whether there is error information. For example, return value, exists, return value, ok, return value, err, etc.
When the function returns too many, for example, there are more than 4 return values, these return values should be collected into the container and then returned in the way of returning the container. For example, return values of the same type can be put into slices, and return values of different types can be put into maps.
However, when a function has multiple return values, if one or several return values do not want to be used, you can discard these return values by underscore_. For example, the following f1 function has two return values. When calling this function, the second return value b is discarded, and only the first return value a is retained and assigned to the variable a is assigned to the variable a.
package main import "fmt" func f1() (int, int) { return 1, 2 } func main() { _, x := f1() ("x: %v\n", x) }
Running results
[Running] go run "d:\SynologyDrive\Software Development\go\golang Getting Started with Project Practical\goproject\\pro01\"
x: 2
This is the end of this article about the implementation of the return value of golang function. For more related content of golang function return value, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!