English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang basic tutorials

Golang control statements

Golang functions & methods

Golang structs

Golang slices & arrays

Golang strings(String)

Golang pointers

Golang interfaces

Golang concurrency

Golang exceptions(Error)

Other Golang items

Go Named Return Parameters

In Golang, named return parameters are usually called named parameters. Golang allows specifying names for the return or result parameters of a function in the function signature or definition. Or you can say it is the explicit naming of return variables in the function definition. Essentially, it solves the requirement to mention variable names in the return statement. Using named return parameters or named parameters, only the return keyword can be used at the end of the function to return the results to the caller. When a function must return multiple values, this concept is usually used. Therefore, to make users feel comfortable and enhance code readability, Golang provides this feature.

Declare named return parameters

To declare named results or return parameters, simply use the return type part of the function signature. Here is the general syntax for declaring functions in Golang.

The syntax for declaring a function without named return parameters:

func function_name(Parameter-list)(Return_type){
    // function body.....
}

In this case, Return_Type is optional, it contains the type of the value returned by the function. If Return_Type is used in the function, then the return statement must be used in the function.

The syntax for declaring a function with named return parameters:

func function_name(Parameter-list)(result_parameter1 data__type,result_parameter2 data_type, …。){ 
    //function body….
    //returns
}

this(result_parameter1 data__type, result_parameter2 data_type, ....) is the list of named return parameters and their types. You can declare n named return parameters.

Example:In the following program, the line 'func calculator(a, b int) (mul int, div int)' contains named return parameters. The return statement at the end of the function does not contain any parameters. The Go compiler will automatically return the parameters.

package main
import "fmt"
// Main Method
func main() {
    //Here, the function is called
    //The function returns two values
    m, d := calculator(105, 7)
    fmt.Println("105 x 7 = ", m)
    fmt.Println("105 / 7 = ", d)
}
// Functions with named parameters
func calculator(a, b int) (mul int, div int) {
    //Here, simple assignment is enough
    //and initialize its value
    mul = a * b
    div = a / b
    //Here is the return keyword
    //No result parameters
    return
}

Output:

105 x 7 =  735
105 / 7 =  15

Important Note

  • If all named return parameters have public or the same types, you can specify a public data type. Compare the following code with the example read above to better understand.

    //Functions with named parameters
    func calculator(a, b int) (mul, div int) {

    Here,mulanddivAll variables are of int type. Therefore, you can also declare named parameters with generic data types, such as function variables (i.e.,aandb)

  • Using named return parameters will enhance code readability because you can know the return parameters just by reading the function signature.

  • After using named return parameters, the return statement is usually called"Naked return".

  • By default, Golang defines all named variables with zero values, and the function can use them. If the function does not modify the value, it will automatically return the zero value.

  • If you will useShort declaration operator (:=)If you initialize named return parameters, an error will occur because they have been initialized by the Go compiler. Therefore, you can use simple assignment (=) to assign values to named return parameters.

    //Functions with named parameters
    func calculator(a, b int) (mul int, div int) {
        //Here, it will throw an error
        //Because the parameters have already been defined
        //In the function signature
        mul := a * b
        div := a / b
        //Here is the return keyword
        //No result parameters
        return
    }
  • Named return parameters or naked return statements are only applicable to short function signatures. For longer functions, explicitly return result parameters (without using named return parameters) to maintain code readability.

  • For named return parameters, the 'naked return' statement must be used.

Knowledge related to this:Functions in Golang