From 0cf5cd7a93dc68f8e9370ab32d653475660ed42e Mon Sep 17 00:00:00 2001
From: "grejo.j"
Date: Wed, 14 Dec 2022 19:47:29 +0530
Subject: [PATCH] added example for naked return
---
.../multiple-return-values.go | 14 +++++
.../multiple-return-values.hash | 4 +-
.../multiple-return-values.sh | 5 ++
public/multiple-return-values | 56 +++++++++++++++++--
4 files changed, 73 insertions(+), 6 deletions(-)
diff --git a/examples/multiple-return-values/multiple-return-values.go b/examples/multiple-return-values/multiple-return-values.go
index cb541c4..4ebdc63 100644
--- a/examples/multiple-return-values/multiple-return-values.go
+++ b/examples/multiple-return-values/multiple-return-values.go
@@ -12,6 +12,16 @@ func vals() (int, int) {
return 3, 7
}
+// Go's return values may be named. If so, they are treated
+// as variables defined at the top of the function.
+// A `return“ statement without arguments returns the
+// named return values. This is known as a "naked" return.
+func split(sum int) (x, y int) {
+ x = sum * 4 / 9
+ y = sum - x
+ return
+}
+
func main() {
// Here we use the 2 different return values from the
@@ -24,4 +34,8 @@ func main() {
// use the blank identifier `_`.
_, c := vals()
fmt.Println(c)
+
+ // The split function will return the values of x & y
+ d, e := split(17)
+ fmt.Println(d, e)
}
diff --git a/examples/multiple-return-values/multiple-return-values.hash b/examples/multiple-return-values/multiple-return-values.hash
index 4d4b705..956d103 100644
--- a/examples/multiple-return-values/multiple-return-values.hash
+++ b/examples/multiple-return-values/multiple-return-values.hash
@@ -1,2 +1,2 @@
-c6e4f5dd9c55b5d2aaeb7e939c216ec76f042501
-vZdUvLB1WbK
+57e7f6308442d1364b13c9217eae8f934a72b408
+U87Tk2E0TIt
diff --git a/examples/multiple-return-values/multiple-return-values.sh b/examples/multiple-return-values/multiple-return-values.sh
index 0d4c599..3b38132 100644
--- a/examples/multiple-return-values/multiple-return-values.sh
+++ b/examples/multiple-return-values/multiple-return-values.sh
@@ -2,6 +2,11 @@ $ go run multiple-return-values.go
3
7
7
+7 10
+
+# Naked return statements should be used only in short
+# functions.
+# They can harm readability in longer functions.
# Accepting a variable number of arguments is another nice
# feature of Go functions; we'll look at this next.
diff --git a/public/multiple-return-values b/public/multiple-return-values
index 9fbc33d..b16d8ee 100644
--- a/public/multiple-return-values
+++ b/public/multiple-return-values
@@ -43,7 +43,7 @@ to return both result and error values from a function.
- 
+ 
package main
|
@@ -76,6 +76,26 @@ the function returns 2 int
s.
+
+
+ Go’s return values may be named. If so, they are treated
+as variables defined at the top of the function.
+A `return“ statement without arguments returns the
+named return values. This is known as a “naked” return.
+
+ |
+
+
+
+func split(sum int) (x, y int) {
+ x = sum * 4 / 9
+ y = sum - x
+ return
+}
+
+ |
+
+
@@ -109,11 +129,25 @@ call with multiple assignment.
use the blank identifier _ .
|
-
+ |
_, c := vals()
fmt.Println(c)
+
+ |
+
+
+
+
+ The split function will return the values of x & y
+
+ |
+
+
+
+ d, e := split(17)
+ fmt.Println(d, e)
}
|
@@ -132,7 +166,21 @@ use the blank identifier _
.
$ go run multiple-return-values.go
3
7
-7
+7
+7 10
+
+
+
+
+
+ Naked return statements should be used only in short
+functions.
+They can harm readability in longer functions.
+
+ |
+
+
+
|
@@ -163,7 +211,7 @@ feature of Go functions; we’ll look at this next.