Named Return Parameters in Golang


In Golang, functions can have named return parameters. This feature makes the code more readable and easier to understand. In this article, we'll explore named return parameters in Golang, their benefits, and how to use them.

What are Named Return Parameters?

Named return parameters are the values that a function returns, with their names specified in the function signature. By default, Golang functions return their values in the order they were defined. However, with named return parameters, developers can assign names to the returned values, making the code more readable.

Example

Here's an example of a function with named return parameters −

package main

import (
   "errors"
   "fmt"
)

func divide(dividend float64, divisor float64) (result float64, err error) {
   if divisor == 0 {
      err = errors.New("division by zero")
      return
   }
   result = dividend / divisor
   return
}

func main() {
   result, err := divide(6.0, 3.0)
   if err != nil {
      fmt.Println("Error:", err)
   } else {
      fmt.Println("Result:", result)
   }
}

Output

Result: 2

In this example, the function divide() returns two values, result and err. These values are named in the function signature, which makes the code more readable.

Benefits of Named Return Parameters

Named return parameters offer several benefits in Golang −

  • Improves Readability − Named return parameters make code more readable and easier to understand, especially when functions return multiple values.

  • Reduces Bugs − By naming return parameters, developers can prevent bugs caused by returning values in the wrong order.

  • Easy to Modify − Named return parameters make it easier to modify code because developers don't have to worry about changing the order of returned values.

How to Use Named Return Parameters

To use named return parameters in Golang, specify the names of the returned values in the function signature. The names are separated from the types by a space, and the types are enclosed in parentheses.

Example

Here's an example of a function that calculates the area and perimeter of a rectangle using named return parameters −

package main

import "fmt"

func rectangleDimensions(length float64, width float64) (area float64, perimeter float64) {
   area = length * width
   perimeter = 2 * (length + width)
   return
}

func main() {
   area, perimeter := rectangleDimensions(5.0, 10.0)
   fmt.Println("Area:", area)
   fmt.Println("Perimeter:", perimeter)
}

Output

Area: 50
Perimeter: 30

In this example, the function rectangleDimensions() returns two values, area and perimeter, which are named in the function signature.

Conclusion

Named return parameters are a useful feature in Golang that make code more readable and easier to understand. By naming the returned values, developers can prevent bugs and make code easier to modify.

Updated on: 26-Apr-2023

985 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements