Go function multiple return values

In this example, we will show you how to write a function in Go to return multiple values.

Go functions allow to return of multiple values.

Go function multiple return values

In the example, we have a threerandom() function, which returns three random values.


package main

import (
    "fmt"
    "math/rand"
    "time"
)

func threerandom() (int, int, int) {

    rand.Seed(time.Now().UnixNano())
    x := rand.Intn(10)
    y := rand.Intn(10)
    z := rand.Intn(10)

    return x, y, z
}

func main() {

    r1, r2, r3 := threerandom()

    fmt.Println(r1, r2, r3)
}

Output:

0 8 7

Comments