Named return values

Go's return values may be named. If so, they are treated as variables defined at the top of the function.

These names should be used to document the meaning of the return values.

A return statement without arguments returns the named return values. This is known as a "naked" return.

Naked return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.

named-results.go

  1. package main
  2. import "fmt"
  3. func split(sum int) (x, y int) {
  4. x = sum * 4 / 9
  5. y = sum - x
  6. return
  7. }
  8. func main() {
  9. fmt.Println(split(17))
  10. }