Go has built-in support for multiple return values.
This feature is used often in idiomatic Go, for example
to return both result and error values from a function.
|
|
|

package main
|
|
import "fmt"
|
The (int, int) in this function signature shows that
the function returns 2 int s.
|
func vals() (int, int) {
return 3, 7
}
|
Go’s return values may be named. If so, they are treated
as variables defined at the top of the function.
A `return“ statement without arguments returns the
named return values. This is known as a “naked” return.
|
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
|
|
func main() {
|
Here we use the 2 different return values from the
call with multiple assignment.
|
a, b := vals()
fmt.Println(a)
fmt.Println(b)
|
If you only want a subset of the returned values,
use the blank identifier _ .
|
_, c := vals()
fmt.Println(c)
|
The split function will return the values of x & y
|
d, e := split(17)
fmt.Println(d, e)
}
|