Go functions can return more than one value.

multiple return A function signature can list several return types, and callers assign each returned value.

Multiple Return Values

total
multi_return.go
Replay: real traced execution (multi-file project)
package main

import "fmt"

func divide(total int, parts int) (int, int) {
	quotient := total / parts
	remainder := total % parts
	return quotient, remainder
}

func main() {
	var total = 17
	quotient, remainder := divide(total, 5)

	fmt.Println("total=", total)
	fmt.Println("quotient=", quotient)
	fmt.Println("remainder=", remainder)
}
package main

import "fmt"

func divide(total int, parts int) (int, int) {
	quotient := total / parts
	remainder := total % parts
	return quotient, remainder
}

func main() {
	var total = 9
	quotient, remainder := divide(total, 5)

	fmt.Println("total=", total)
	fmt.Println("quotient=", quotient)
	fmt.Println("remainder=", remainder)
}
package main

import "fmt"

func divide(total int, parts int) (int, int) {
	quotient := total / parts
	remainder := total % parts
	return quotient, remainder
}

func main() {
	var total = 20
	quotient, remainder := divide(total, 5)

	fmt.Println("total=", total)
	fmt.Println("quotient=", quotient)
	fmt.Println("remainder=", remainder)
}
  1. total ← 17

    11func main() {12  var total→ 17 = 17 //@total=9, 2013  quotient, remainder := divide(total17, 5)
  2. quotient ← 3, remainder ← 2

    5func divide(total17 int, parts5 int) (int, int) {6  quotient→ 3 := total17 / parts57  remainder→ 2 := total17 % parts58  return quotient3, remainder29}
  3. quotient ← 3, remainder ← 2

    12  var total = 17 //@total=9, 2013  quotient→ 3, remainder→ 2 := divide(total17, 5)1415  fmt.Println("total=", total17)16  fmt.Println("quotient=", quotient3)17  fmt.Println("remainder=", remainder2)18}
    outputtotal= 17
    quotient= 3
    remainder= 2
  1. total ← 9

    11func main() {12  var total→ 9 = 913  quotient, remainder := divide(total9, 5)
  2. quotient ← 1, remainder ← 4

    5func divide(total9 int, parts5 int) (int, int) {6  quotient→ 1 := total9 / parts57  remainder→ 4 := total9 % parts58  return quotient1, remainder49}
  3. quotient ← 1, remainder ← 4

    12  var total = 913  quotient→ 1, remainder→ 4 := divide(total9, 5)1415  fmt.Println("total=", total9)16  fmt.Println("quotient=", quotient1)17  fmt.Println("remainder=", remainder4)18}
    outputtotal= 9
    quotient= 1
    remainder= 4
  1. total ← 20

    11func main() {12  var total→ 20 = 2013  quotient, remainder := divide(total20, 5)
  2. quotient ← 4, remainder ← 0

    5func divide(total20 int, parts5 int) (int, int) {6  quotient→ 4 := total20 / parts57  remainder→ 0 := total20 % parts58  return quotient4, remainder09}
  3. quotient ← 4, remainder ← 0

    12  var total = 2013  quotient→ 4, remainder→ 0 := divide(total20, 5)1415  fmt.Println("total=", total20)16  fmt.Println("quotient=", quotient4)17  fmt.Println("remainder=", remainder0)18}
    outputtotal= 20
    quotient= 4
    remainder= 0

Follow the Two Results

  1. divide receives total and parts.
  2. It calculates a quotient.
  3. It calculates a remainder.
  4. return quotient, remainder sends both values back.
  5. The caller stores them in two names. | Returned value | Caller variable | | --- | --- | | first value | quotient | | second value | remainder |

Exercise: multi_return.go

Return both quotient and remainder from a divide helper and print both caller variables