×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Go Pointers

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Go Function Returns

Last Updated : July 21, 2025

In Go, a function can return one or more values after its execution.

Function Return in Go

You define the return type(s) after the parameter list. You can return values using the return keyword followed by the value(s).

Syntax

The following is the general syntax for defining a function in Go with parameters and a return type:

func functionName(parameters) returnType {
    // function body
    return value
}

Single Value Return

Just like other programming language, Go function can also return a single value.

Example

The following example demonstrates how to define a function that takes two integer parameters and returns their sum:

package main
import "fmt"

func add(a int, b int) int {
    return a + b
}

func main() {
    sum := add(10, 20)
    fmt.Println("Sum:", sum)
}

When you run the above code, the output will be:

Sum: 30

Multiple Values Return

Go supports returning multiple values from a function using tuple-style return.

Example

The following example shows how to define and use a function that returns multiple values-in this case, the quotient and remainder of a division:

package main
import "fmt"

func divide(a int, b int) (int, int) {
    quotient := a / b
    remainder := a % b
    return quotient, remainder
}

func main() {
    q, r := divide(17, 5)
    fmt.Println("Quotient:", q)
    fmt.Println("Remainder:", r)
}

When you run the above code, the output will be:

Quotient: 3
Remainder: 2

Named Return Values

You can name the return variables and use a naked return to return the current values.

Example

The following example demonstrates how to use named return values in a function, allowing you to return results without explicitly specifying them in the return statement:

package main
import "fmt"

func rectangle(dim1, dim2 int) (area int, perimeter int) {
    area = dim1 * dim2
    perimeter = 2 * (dim1 + dim2)
    return
}

func main() {
    a, p := rectangle(4, 5)
    fmt.Println("Area:", a)
    fmt.Println("Perimeter:", p)
}

When you run the above code, the output will be:

Area: 20
Perimeter: 18

Return with Error Handling

It is common in Go to return a value and an error from a function to handle failure cases gracefully.

Example

The following example shows how to return an error from a function, demonstrating safe division with error handling for division by zero:

package main
import (
    "errors"
    "fmt"
)

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

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

When you run the above code, the output will be:

Error: division by zero

Use Cases

  • Returning a single computation result.
  • Returning multiple related values (like result and error).
  • Using named returns to simplify logic.

Exercise

Test your understanding of Go function returns.

  1. Which keyword is used to return values from a function?
  2. How many values can a Go function return?
  3. What is a common pattern used when returning values in Go?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.