Go Function return multiple values

Introduction

Go can return multiple values from a function.

Here is an example function that returns two integers:

func f() (int, int) { 
    return 5, 6 
} 

Assign returned value to two variables


package main/*from ww w  .  j  a v a  2 s  . c  o  m*/

import "fmt" 

func main() { 
    x, y  := f() 
    fmt.Println(x) 
    fmt.Println(y) 
} 


func f() (int, int) { 
    return 5, 6 
} 

Multiple values are often used to return an error value along with the result (x, err := f()), or a boolean to indicate success (x, ok := f()).




PreviousNext

Related