How to Return Multiple Values From a Function in Golang

Go provides the capability for programmers to make functions have a lot of return value. In this post we will discuss how to apply it.

Crazy Gopher

#gopher lover. Anything in golang

Published · Nov 10 2019

Generally a function has only one return value. If there is a need where the data returned must be large, usually use types such as map, slice, or struct as a return value.
Go provides the capability for programmers to make functions have a lot of return value. In this chapter we will discuss how to apply it.

Multiple Return Function

How to make a function that has a lot of return value is not difficult. All you have to do is just write it when you declare the function of all data type values ​​that are returned, and in the keywords returnwrite all the data you want to return. An example can be seen in the following.
package main import "fmt" import "math" func calculate(d float64) (float64, float64) { // hitung luas var area = math.Pi * math.Pow(d / 2, 2) // hitung keliling var circumference = math.Pi * d // kembalikan 2 nilai return area, circumference }

The calculate()above function accepts one parameter ( diameter) which is used in the calculation process. In the function there are 2 things that are calculated, namely the value of the circumference and circle . The two values ​​are then used as a function return value.

How to define many return values ​​can be seen in the code above, immediately write the data type, all the return values ​​are separated by commas, then parentheses are added.
func calculate(d float64) (float64, float64)

Do not forget in the writing of keywords returnmust also be written all the data used as return values ​​(with comma separator).
return area, circumference

The implementation of the calculate()above functions can be seen in the following code.
func main() { var diameter float64 = 15 var area, circumference = calculate(diameter) fmt.Printf("luas lingkaran\t\t: %.2f \n", area) fmt.Printf("keliling lingkaran\t: %.2f \n", circumference) }

Program output:


Because the function has many return values, it must also prepare a lot of variables to accommodate the return value (according to the total return value of the function).
var area, circumference = calculate(diameter)
. . .

Functions with Predefined Return Value

Another uniqueness that is rarely found in other languages ​​is, in the Go variable used as a return value can be defined at the beginning.
func calculate(d float64) (area float64, circumference float64) { area = math.Pi * math.Pow(d / 2, 2) circumference = math.Pi * d return }

calculate Our modified function has become simpler. Can be seen in the code above, there are quite a lot of differences compared to the calculate previous function . Consider the following code.
func calculate(d float64) (area float64, circumference float64) {

Declared function has 2 types of data types, and variables that will later be used as return values ​​are also declared. Variables area of type float64, and circumference type float64.

Because the return value variable is pre-determined, to return the value simply calls return without any variable being followed. The last value area and circumference before the keyword call return is the result of the above function.
Gufran Mirza

Aug 4 2019

Write your response...

On a mission to build Next-Gen Community Platform for Developers