
In Golang, named return parameters and values allow you to specify the names of the return values in the function signature.
This feature was introduced in Go 1.4 and has since become a popular way to improve code readability and maintainability.
With named return parameters, you can return multiple values from a function and assign them to specific variables in the calling code.
For example, the `math.Min` function returns two values: the minimum value and the index of the minimum value in the slice.
If this caught your attention, see: Golang Return Multiple Values
Declaring Parameters
Declaring Parameters can be done by specifying the type of each return value in parentheses after the function's parameter list.
You can declare multiple named return parameters, and the Go compiler will automatically return their values when you use the return statement without any parameters.
The named return argument list can contain n number of parameters, each with its own data type.
For example, in the function calculator(a, b int) (mul int, div int), two named return parameters are declared with their respective data types.
The return statement at the end of the function can be empty, as the Go compiler will automatically return the named parameters.
Recommended read: Golang List
Understanding Values
Named return values can make a significant difference in code clarity and performance. They can eliminate the need for explicit return statements at the end of a function.
The code generated by the compiler can be drastically reduced in size, as seen in the example where the function size was reduced from 243 to 67 bytes. This reduction in size can also save CPU cycles upon exiting the function.
Named return values can simplify the function implementation by providing a clear understanding of what each return value represents. This can make the code more readable and maintainable.
The naked return feature of Golang can be used to achieve the same benefit as named return values, but without explicitly returning values. This can be done by simply using the return keyword without specifying the return values.
Named return values can also provide default values for the returned variables. This can be useful when the function is called without any arguments.
You might enjoy: Golang Function with Many Parameters
Working with Values
Giving a return value a name can make a huge difference in code efficiency. It reduces the size of the function down from 243 to 67 bytes, and saves CPU cycles upon exiting out.
Named return values can improve code clarity by providing a clear understanding of what each return value represents. This can be achieved by simply specifying the names and types of the variables in the function signature, followed by the return keyword.
Eliminating the need to explicitly return values at the end of the function can simplify the function implementation. This is especially true for trivial cases, where the function size can be reduced from 243 to 67 bytes.
Named return values also allow you to set default values, which means that if the function is called without any arguments, it will return the default values specified in the function signature.
Example and Output
In Go, named return values allow you to return multiple values from a function.
We can define a function with named return values, as seen in the swapDefaults function, which returns two named integers, x and y.
The return values can be assigned to variables in the calling function, as demonstrated in the main function where x and y are assigned the returned values of 20 and 10 respectively.
You can also call a function with named return values without any arguments, and it will return the default values set within the function.
Minio Server Example
In the Minio server example, a function header is examined to demonstrate the impact of using a named return parameter.
By modifying the code to use a named return parameter, the function size is reduced by 300 bytes out of a total of 1150 bytes.
This change is a significant improvement, especially considering it's a minimal alteration to the source code.
The modified code has a "cleaner" look, which may be a preferable choice for some developers.
The variable "ch" is a normal local variable, just like any other defined within the function, and can be changed from its default state.
Worth a look: Golang Variable Naming Convention
Output

The output of our code is where the magic happens.
We define a function called swapDefaults that has no parameters and returns two named integers, x and y.
Inside the function, we set the value of x to 20 and the value of y to 10.
We then use the return keyword to return the named variables x and y.
The function returns 20 and 10 respectively, which we assign to the variables x and y in the main function.
We then print these values to the console, and the output is what we expect.
Conclusion
As we've seen, named return values are becoming a more prominent feature in Go programming.
We can expect to see a gradual adoption of named return values in both new and existing code.
The Go team is also exploring the development of a utility to help automate the process of modifying code to use named return values.
This utility would work similarly to gofmt, but would modify the source code automatically to make the necessary changes.
Featured Images: pexels.com

