Returning multiple values from a function in Go

In the previous post we looked at how we can create and use functions in go.

Now lets give a look at how we can return multiple values from a function and use them.
We will modify the function in the previous post to return both minimum and maximum value in the array

package main

import "fmt"

func main() {    
 x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}    
    minimum, maximum := calculateMinimumAndMaximum(x)
    fmt.Println("Minimum value is", minimum)
    fmt.Println("Maximum value is", maximum)   
}


func calculateMinimumAndMaximum(x []int) (int, int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
        
        if max < v {
         
            max = v
        }
    }
    return minimum, max 
}

In the code above, we have a function calculateMinimumAndMaximum which returns two integer values, the minimum and maximum number in the array list.

Now, it is fairly evident in the above code that of the two values returned the first is minimum and the second is maximum. How did we know that? Because we can look at the last return statement of the function, which return minimum and maximum.

But if we do not have access to the source code but know only the method signature

  func calculateMinimumAndMaximum(x []int) (int,int)

how can we know what is the first value and what is the second value.

Go comes to rescue here! We can specify names for our return values in our method signature

so our signature

  func calculateMinimumAndMaximum(x []int) (int,int)

can be converted to

  func calculateMinimumAndMaximum(x []int) (min int, maximum int)

Now we know the first value is minimum and second value is maximum from signature itself.

Here is how the final code looks like now.


package main

import "fmt"

func main() {    
 x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}    
    minimum, maximum := calculateMinimumAndMaximum(x)
    fmt.Println("Minimum value is", minimum)
    fmt.Println("Maximum value is", maximum)   
}


func calculateMinimumAndMaximum(x []int) (min int, maximum int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
        
        if max < v {
         
            max = v
        }
    }
    return minimum, max 
}


Pretty Awesome!

~~ Whizdumb ~~

Advertisements

One thought on “Returning multiple values from a function in Go

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s