GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing gobyexample.com — Go by Example: Multiple Return Values

Go by Example: Multiple Return Values

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 ints.

func vals() (int, int) {
    return 3, 7
}
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)
}
$ go run multiple-return-values.go
3
7
7

Accepting a variable number of arguments is another nice feature of Go functions; we’ll look at this next.

Next example: Variadic Functions.

by Mark McGranaghan and Eli Bendersky | source | license

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.