📜  在 Golang 中命名函数的返回值

📅  最后修改于: 2021-10-24 13:27:42             🧑  作者: Mango

返回值有助于在执行函数体中给出的指令后保留函数的最终输出。 Golang 中的函数在返回值方面表现出多样性,并且依赖于程序员来决定是否命名它们。
Golang 引入了“裸返回”的概念,允许使用 return 关键字,而无需在函数体中明确声明返回值,前提是返回值在函数头中声明。但是,变量名称必须与函数头中定义的名称相同。

package main
  
import "fmt"
  
// This function returns a string message by taking a parameter
// "name" of string type. The Naked return concept is being
// displayed here since the return statement not specify the return
// of message variable. The returning of the message variable
// has already being defined 
func greeting(name string) (message string) {
  
    // Since the variable is already 
    // defined in function variable
    // there is no need to declare it 
    // again in the function body
    // using " := " operator.
  
    // The name "message" must be same as 
    // the one declared in the function header.
    message = "Hello, " + name
      
    // This statement returns message variable
    // without explicitly stating its name
    return 
}
  
// Driver code
func main() {
  
    // The return value of greeting 
    // function is stored in msg variable
    msg := greeting("GFG")
      
    // Printing output
    fmt.Println(msg) 
}

输出:

Hello, GFG

此外,该函数可以返回一个变量而无需在函数头中指定其名称,而仅指定其数据类型。

package main
  
import "fmt"
  
// This function returns a string datatype
// Since the return variable is not 
// defined in the function header,
// There is an explicit need to specify
// the variable to return
  
func greeting(name string) string {
    message := "Hello, " + name
    return message
}
  
// Driver code
func main() {
    msg := greeting("GFG")
    fmt.Println(msg)
}

输出:

Hello, GFG

一个函数也可以返回多个值并接收多个参数。然而,与返回多个值的函数工作,变量的数目该函数返回必须等于保持返回值当函数执行完毕变量的数目。

package main
  
import "fmt"
  
// The below function returns four
// variables after performing operations
// on the two parameters that were passed to it
  
func operations(a, b int) (sum, diff, prod, div int) {
  
    // The four variables to be returned are defined here
  
    sum, diff, prod, div = a+b, a-b, a*b, a/b
  
    // returning the above-defined four variables.
    return
}
  
// Driver code
func main() {
    a, b := 5, 3
  
    // Since the function returns four values,
    // Four receiver variables must be used.
    sum, diff, prod, div := operations(a, b)
    fmt.Println(sum, diff, prod, div)
}

输出:

8 2 15 1