Go functions can return multiple different types of values. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: func getDetails() (int, string). In practice, a function that calculates the area of a circle can return the area and an optional error: func circleArea(radius float64) (float64, error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.
The type of return value of Go function
In Go language, a function can return multiple values, and each value Can be of different types. The type of the return value is specified in the function signature and can be returned by using the return
statement.
Syntax
func funcName(param1 type1, param2 type2) (return1 type1, return2 type2)
For example, the following function returns an integer and a string:
func getDetails() (int, string) { return 1, "John Doe" }
Practical case
Consider a function that calculates the area of a circle:
import "math" func circleArea(radius float64) (float64, error) { if radius < 0 { return 0, errors.New("radius cannot be negative") } return math.Pi * radius * radius, nil }
This function returns two values: the area of the circle and an optional error that is returned if the radius is negative.
Notes
()
. naked
return statement to return the value directly, but this usage is mainly used for low-level system programming. return
statements with explicit type declarations. The above is the detailed content of What is the type of return value of Golang function?. For more information, please follow other related articles on the PHP Chinese website!