Golang: Function Multi-Value Returns

By Xah Lee. Date: .

A function can return more than 1 value. example

func ff() (int, int, int) { return 3, 4, 5 }

To get these values, assign them to multiple variables, like this:

var a, b, c = ff()

Function with multiple return values is frequently used in golang. The second value is usually a error state.

package main

import "fmt"

// returns 2 values
func ff(x, y int) (int, int) {
	return x, y
}

func main() {
	var a, b = ff(3, 4)
	fmt.Println(a, b)
}

// prints 3 4

golang function