summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSam Zaydel <szaydel@gmail.com>2014-06-22 06:20:12 -0700
committerSam Zaydel <szaydel@gmail.com>2014-06-22 06:20:12 -0700
commite5d8895c8e837ac8fd0b7272195d0d99afbf589b (patch)
tree2dbaa2f3f1691048f37b8f7e8fc5169db1469265
parent9862212ed15a4b28c946a7b00cd3624d00971b97 (diff)
Should have more detail about named return values.
There was not a section about named return values, and it feels like it is a valuable and important enough thing to learn early on. If nothing else, when looking at someone else's code this may be a point of confusion.
-rw-r--r--go.html.markdown10
1 files changed, 10 insertions, 0 deletions
diff --git a/go.html.markdown b/go.html.markdown
index bb6b04eb..2e68a25c 100644
--- a/go.html.markdown
+++ b/go.html.markdown
@@ -116,6 +116,16 @@ can include line breaks.` // Same string type.
learnFlowControl() // Back in the flow.
}
+// It is possible, unlike in many other languages for functions on go
+// to have named return values.
+// We just have to assign a name to the type being returned in the function
+// declaration line. This allows us to easily return from multiple points
+// in a function as well as to only use the return keyword, without anything further.
+func learnNamedReturns(x, y int) (z int) {
+ z = x * y
+ return // z is implicit here, because we named it earlier.
+}
+
// Go is fully garbage collected. It has pointers but no pointer arithmetic.
// You can make a mistake with a nil pointer, but not by incrementing a pointer.
func learnMemory() (p, q *int) {