100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Multiple Return Values in Go

Understand how Go functions return multiple values, including the idiomatic error-handling pattern.

FunctionsBeginner7 min readJul 8, 2026
Analogies

Introduction

Unlike many languages that only allow a single return value, Go functions can return multiple values at once. This feature is widely used for returning a result alongside an error, avoiding the need for exceptions or output parameters that other languages rely on.

🏏

Cricket analogy: Like an umpire's decision returning both "out" and the reason (LBW, caught) in one signal, Go functions return a result and an error together instead of just a lone verdict.

Syntax

go
func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, fmt.Errorf("division by zero")
    }
    return a / b, nil
}

Explanation

When a function returns more than one value, the return types are wrapped in parentheses. The caller must handle all returned values, typically using multiple assignment such as result, err := divide(10, 2). Go also supports named return values, where the return variables are declared in the signature itself, e.g. func split(sum int) (x, y int). With named returns, a naked return statement (just 'return' with no arguments) sends back the current values of those named variables.

🏏

Cricket analogy: Like a scoreboard function declared to return (runs, wickets int) up front, a naked return at the end of the over sends back whatever runs and wickets were already tallied, no need to restate them.

Example

go
package main

import (
    "errors"
    "fmt"
)

func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return // naked return
}

func main() {
    result, err := divide(10, 2)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Result:", result)
    }

    a, b := split(17)
    fmt.Println(a, b)
}

// Output:
// Result: 5
// 7 10

Key Takeaways

  • Multiple return types are declared in parentheses, e.g. (int, error).
  • The (value, error) pattern is Go's idiomatic replacement for exceptions.
  • Named return values let you declare result variables in the function signature.
  • A naked return sends back the current values of named return variables.
  • Callers should check the error before trusting the other returned values.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#MultipleReturnValuesInGo#Multiple#Return#Values#Syntax#StudyNotes#SkillVeris