Golang 如何从函数返回多个值

Golang 如何从函数返回多个值

在本教程中,我们将看到如何通过算法和示例从Golang函数中返回多个值。与其他编程语言(如Python)一样,Golang也支持从函数中返回多个值。在第一个示例中,我们将在函数中传递两个数字,然后一起返回较小的数字和较大的数字。在第二个示例中,我们传递两个数字,一次返回加法、减法、乘法和除法。

语法

Func functionName(arguments) (returnType1, returnType2, …) {

   // logic
   return returnTypeVariable1, returnTypeVariable2, …
}

解释

(返回类型1, 返回类型2, …) – 在这行代码中,我们可以将返回值的数据类型写在返回类型1和返回类型2的位置。

返回 returnTypeValue1, returnTypeValue2, … – 在这里,我们返回相应数据类型的变量。

步骤

  • 步骤1 - 声明变量。

  • 步骤2 - 初始化变量。

  • 步骤3 - 调用返回多个值的函数。

  • 步骤4 - 打印结果。

示例1

在这个示例中,我们将调用一个函数,并将两个整数作为参数。这个函数将先返回较小的数,然后返回较大的数。

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)
func smallerNumber(number1, number2 int) (int, int) {
   if number1 < number2 {
      return number1, number2
   }
   return number2, number1
}
func main() {

   // declaring the variable
   var number1, number2 int

   // initializing the variable
   number1 = 10
   number2 = 21
   fmt.Println("Golang program to return the multiple values from the function in Golang.")

   // calling the recursive function
   smaller, bigger := smallerNumber(number1, number2)
   fmt.Println("The smaller number is", smaller)
   fmt.Println("The bigger number is", bigger)
}

输出

Golang program to return the multiple values from the function in Golang.
The smaller number is 10
The bigger number is 21

示例2

在这个示例中,我们将编写一个函数,它将一次返回加法、减法、乘法和除法的结果。这种方法将减少程序中的函数调用次数。

package main
import (

   // fmt package provides the function to print anything
   "fmt"
)
func addSubMulDiv(number1, number2 int) (int, int, int, int) {
   var addition, subtraction, multiplication, division int

   // adding two numbers
   addition = number1 + number2

   // subtracting two numbers
   subtraction = number1 - number2

   // multiplying two numbers
   multiplication = number1 * number2

   // dividing two numbers
   division = number1 / number2
   return addition, subtraction, multiplication, division
}
func main() {

   // declaring the variable
   var number1, number2 int

   // initializing the variable
   number1 = 100
   number2 = 20
   fmt.Println("Golang program to return the multiple values from the function in Golang.")

   // calling the recursive function
   addition, subtraction, multiplication, division := addSubMulDiv(number1, number2)
   fmt.Printf("The addition of %d and %d is %d.\n", number1, number2, addition)
   fmt.Printf("The subtraction of %d and %d is %d.\n", number1, number2, subtraction)
   fmt.Printf("The multiplication of %d and %d is %d.\n", number1, number2, multiplication)
   fmt.Printf("The division of %d and %d is %d.\n", number1, number2, division)
}

输出

Golang program to return the multiple values from the function in Golang.
The addition of 100 and 20 is 120.
The subtraction of 100 and 20 is 80.
The multiplication of 100 and 20 is 2000.
The division of 100 and 20 is 5.

结论

这是在Golang中从函数返回多个值的方法,附带两个示例。要了解更多关于Golang的内容,您可以查看这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程