Home > Article > Backend Development > How to get the return value of golang function?
Go language functions pass data through return values. To return a single value, simply specify the return value type in the function signature and use a variable to receive the return value when the function is called. To return multiple values, you need to use a tuple type in the function signature and use multiple variables to receive the return values when calling the function.
In the Go language, functions can pass data through return values. A function can return a single value, or multiple values, and the type of the return value must be specified in the function signature.
To return a single value, simply specify the return value type in the function signature as follows:
func myFunction() int { return 10 }
When calling the function, you can use variables Receive return value:
num := myFunction() fmt.Println(num) // 输出:10
To return multiple values, you need to use a tuple type in the function signature, as shown below:
func myFunction() (int, string) { return 10, "Hello" }
In calling function, you can use multiple variables to receive return values:
num, str := myFunction() fmt.Println(num, str) // 输出:10 Hello
The following is an example of a function that calculates the area of a rectangle:
func calculateArea(length, width float64) float64 { return length * width }
In the main function , we can use this function and print the return value:
func main() { length := 5.0 width := 2.5 area := calculateArea(length, width) fmt.Println("矩形的面积为:", area) // 输出:矩形的面积为: 12.5 }
The above is the detailed content of How to get the return value of golang function?. For more information, please follow other related articles on the PHP Chinese website!