Function Returning Multiple Values in Go Language


Go is a modern programming language that offers a wide range of features for developing efficient and reliable software. One such feature is the ability to return multiple values from a function. In this article, we will explore how to use functions that return multiple values in Go.

Introduction to Multiple Value Returns

In Go, a function can return multiple values of different types. This feature is useful when you want to return more than one value from a function, such as returning both the result of a computation and an error value that indicates whether the computation was successful.

To define a function that returns multiple values, you simply specify the types of the return values separated by commas in the function signature. For example, consider the following function that takes two integers as input and returns their sum and difference −

Example

package main

import "fmt"

func sumAndDiff(a, b int) (int, int) {
   return a+b, a-b
}

func main() {
   sum, diff := sumAndDiff(10, 5)
   fmt.Println("Sum:", sum, "Diff:", diff)
}

Output

Sum: 15 Diff: 5

In this example, the function signature specifies that the function returns two integers (int, int).

Using Multiple Value Returns

To use the values returned by a function that returns multiple values, you can use the special syntax for multiple assignments in Go. For example, to use the sumAndDiff() function defined earlier, you can do the following −

Example

package main

import "fmt"

func sumAndDiff(a, b int) (int, int) {
   return a+b, a-b
}

func main() {
   sum, diff := sumAndDiff(10, 5)
   fmt.Printf("Sum: %d, Diff: %d", sum, diff)
}

Output

Sum: 15, Diff: 5

In this example, we've used the sumAndDiff() function to compute the sum and difference of 10 and 5. The values returned by the function are assigned to the variables sum and diff using the special syntax for multiple assignments. We then print the values using the fmt.Printf() function.

Returning Named Values

In addition to returning multiple values of different types, Go also allows you to return named values from a function. Named values are useful when you want to return a set of related values that have a common meaning.

To define a function that returns named values, you simply specify the names of the return values in the function signature. For example, consider the following function that takes a string as input and returns its length and a boolean value that indicates whether the string is empty or not −

Example

package main

import "fmt"

func lenAndEmpty(s string) (length int, isEmpty bool) {
   length = len(s)
   isEmpty = length == 0
   return
}

func main() {
   str := "Hello World!"
   length, isEmpty := lenAndEmpty(str)
   fmt.Printf("Length: %d, Is Empty: %t\n", length, isEmpty)
}

Output

Length: 12, Is Empty: false

In this example, we've specified the names length and isEmpty for the return values in the function signature. We then compute the length of the input string and check if it's empty, and assign the values to the named return values.

Using Named Values

To use the named return values from a function, you can simply call the function and access the values by name. For example, to use the lenAndEmpty() function defined earlier, you can do the following −

Example

package main

import "fmt"

func lenAndEmpty(s string) (length int, isEmpty bool) {
   length = len(s)
   isEmpty = length == 0
   return
}

func main() {
   length, isEmpty := lenAndEmpty("hello")
   fmt.Printf("Length: %d, IsEmpty: %t", length, isEmpty)
}

Output

Length: 5, IsEmpty: false

In this example, we've used the lenAndEmpty() function to compute the length and emptiness of the string "hello". The values returned by the function are accessed by name using the variables length and isEmpty. We then print the values using the fmt.Printf() function.

Conclusion

Functions that return multiple values are a powerful feature of Go that can help you write concise and expressive code. With this feature, you can return more than one value from a function and use them in a meaningful way. By understanding how to use functions that return multiple values, you can improve the efficiency and reliability of your Go programs.

Updated on: 18-Apr-2023

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements