Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The return statement in Go terminates the execution of the current function and transfers control back to the calling function. If the function’s signature specifies return types, the return statement must provide expressions that evaluate to values matching the function signature’s declared return types.

Syntax

return [expressionList]

Structural Mechanics

1. Terminating Statements Functions declared without a return type (void functions) do not require a return statement at the end of their block. An empty return can be used to halt execution prematurely. Functions declaring one or more return types must end with a terminating statement. While an explicit return is the most common way to satisfy this, Go’s compiler accepts any valid terminating statement. This includes a call to a built-in terminating function (like panic), an infinite loop (for {}), a goto statement, or a switch/select block where all possible execution paths end in a terminating statement.
package main

import "fmt"

// Implicit return at the end of the block
func logMessage(msg string) {
    fmt.Println(msg)
}

// Explicit empty return to halt execution
func haltEarly(condition bool) {
    if condition {
        return 
    }
    fmt.Println("Continued")
}

func main() {
    logMessage("System starting...")
    haltEarly(true)  // Will return immediately
    haltEarly(false) // Will print "Continued"
}
2. Multiple Return Values Go natively supports multiple return values. The expressionList typically contains comma-separated expressions that map positionally to the types defined in the function signature. Alternatively, Go allows a single expression in a multiple-return statement if that expression is a call to a multi-valued function whose return types perfectly match the caller’s return signature.
package main

import "fmt"

// Returns comma-separated expressions
func getCoordinates() (int, int) {
    return 10, 20 
}

// Returns a single expression that yields multiple values
func forwardCoordinates() (int, int) {
    return getCoordinates() 
}

func main() {
    x, y := forwardCoordinates()
    fmt.Printf("X: %d, Y: %d\n", x, y)
}
3. Named Return Values and Naked Returns If a function signature assigns identifiers to its return values, Go treats them as variables defined at the top of the function block, initialized to their respective zero values. A return statement without an expressionList in such a function is called a naked return. It implicitly yields the current state of those named variables.
package main

import "fmt"

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return // Implicitly returns the current values of x and y
}

func main() {
    a, b := split(18)
    fmt.Printf("Split: %d, %d\n", a, b)
}
Note: Even with named return values, you can still provide an explicit expressionList to override the named variables at the point of return.

Execution Order and defer

The return statement is not an atomic operation. When a return statement is encountered, Go executes the return sequence in a specific, deterministic order:
  1. Evaluation: The expressions in the expressionList are evaluated.
  2. Assignment: The evaluated results are assigned to the function’s return variables (either named or implicit).
  3. Deferred Execution: Any functions scheduled via the defer keyword are executed in Last-In-First-Out (LIFO) order.
  4. Yield: Control is officially transferred back to the caller.
Because deferred functions execute after the return variables are assigned but before control is yielded, a deferred closure can mutate named return variables.
package main

import "fmt"

func increment() (i int) {
    defer func() { i++ }()
    return 1 // 1 is assigned to i, then defer increments i to 2. The caller receives 2.
}

func main() {
    result := increment()
    fmt.Printf("Result: %d\n", result)
}
Master Go with Deep Grasping Methodology!Learn More