Examples
After the function parameters, we can specify named return variables in round brackets. Here is an example of a named return type in Go.
Code 1
package main
import "fmt"
func example(A, B, C int) (a, b, c int) {
a = A + 5
b = B + 10
c = C + 5
return
}
func main() {
A, B, C := example(10, 100, 1000)
fmt.Println(A, B, C)
}
The output of the above program:
15 110 1005
Code 2
package main
import "fmt"
func rect(l int, b int) (area int) {
var parameter int
parameter = 2 * (l + b)
fmt.Println("The Parameter: ", parameter)
area = l * b
return // Return statement without specified variable name
}
func main() {
fmt.Println("The Area: ", rect(10, 20))
}
The output of the above program:
The Parameter: 60
The Area: 200
Features
- We don't have to manually allocate the return variables since they're named as result types; they're automatically initialised to zero when the function runs.
- Named return parameters serve as excellent implicit documentation. We can tell what the function returns right from its signature, and naming things makes it easier to understand the code.
- If a function has numerous exit points, we don't need to write all the parameters; we simply return (called bare return). We can still list the returned values directly, and we should probably do so to make the code easier to read.
Uses
As various programmers have pointed out, the advantage of named return values is that they can be used in closures (i.e., defer statements). As a result, the named return value in a function called as a result of a defer statement can be accessed and acted correctly.
Let’s consider an example named myFun():
func myFun(msg string) (i int, e error) {
i = 13
defer func() {
if r := recover(); r != nil {
e = fmt.Errorf("Recovered Error: %v", r)
}
}()
if msg == "panic" {
panic("Panic!!")
}
i = 23
return
}
We create a deferred function call to call recover() and set e to the recovered error value.
If the string parameter msg equals panic, the panic() function is called.
Otherwise, we call return and set the value of i to 23.
Now, if we call the function with i as e:= myFunc("don't panic"), we get the following result:
If we call the function as i, e = myFunc("panic"), we get the following result:
- i: 13
- Err: Recovered Error: Panic!!
We must utilize the named return values technique here because the return values from a deferred function call are discarded.
For this use-case, named return values allow application designers to specify desirable safe/default values that would eventually be returned from the panicked function.
Read Also - Difference between argument and parameter
Frequently Asked Questions
In Golang, what does panic() do?
In Go, the panic() function is equivalent to exceptions raised at runtime when a problem occurs. When a program encounters an unexpected error, panic() is presented, or the programmer throws the exception on purpose to handle specific errors.
In Golang, what is defer?
The defer keyword in Golang is used to postpone the execution of a function or a statement until another function returns. Defer, in simple terms, moves the execution of a statement to the end of a function.
In Go, how do you use recover?
The recover function in Go is used to deal with panic. It's a built-in function in the Go language, defined in the built-in package. This function is used primarily to retake control of a panicking Goroutine.
What do Go routines involve?
Goroutines are functions or procedures that run in parallel with others. Goroutines are similar to lightweight threads. When compared to a thread, the cost of starting a Goroutine is negligible. As a result, Go applications frequently run thousands of Goroutines simultaneously.
Conclusion
This article has discussed the definition, examples, features, and uses of the Golang Named return values. We hope this article has helped you understand the concept of named return Values in Golang.
You can learn related concepts like Object-oriented paradigms, operating systems, classes, inheritance, etc., at the Coding Ninjas Studio blogs vertical.
You can check out this article on Exploring the dynamics of Golang. To understand the usefulness of Golang, read this article Why Golang is so famous among engineers.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!
We wish you Good Luck! Keep coding and keep reading Ninja!!