aboutsummaryrefslogtreecommitdiff
path: root/doc/go_spec.html
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2018-09-21 21:03:35 -0700
committerRobert Griesemer <gri@golang.org>2018-09-24 21:15:27 +0000
commit206fd7886b717d49758de9c125c1fd3575d74ef6 (patch)
tree4e665e8faa01de7a0e71bd6e22c4d4f1263cbb4a /doc/go_spec.html
parentc5a8d1d2f92678b3e17781dd1315f15e24da00f3 (diff)
downloadgo-206fd7886b717d49758de9c125c1fd3575d74ef6.tar.gz
go-206fd7886b717d49758de9c125c1fd3575d74ef6.zip
spec: be more precise about the moment deferred functions are executed
Fixes #27802. Change-Id: I7ea9f7279300a55b0cb851893edc591a6f84e324 Reviewed-on: https://go-review.googlesource.com/136758 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'doc/go_spec.html')
-rw-r--r--doc/go_spec.html14
1 files changed, 9 insertions, 5 deletions
diff --git a/doc/go_spec.html b/doc/go_spec.html
index 57bb3b53f5..32336e86f8 100644
--- a/doc/go_spec.html
+++ b/doc/go_spec.html
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
- "Subtitle": "Version of August 30, 2018",
+ "Subtitle": "Version of September 24, 2018",
"Path": "/ref/spec"
}-->
@@ -5546,7 +5546,10 @@ executes, the function value and parameters to the call are
and saved anew but the actual function is not invoked.
Instead, deferred functions are invoked immediately before
the surrounding function returns, in the reverse order
-they were deferred.
+they were deferred. That is, if the surrounding function
+returns through an explicit <a href="#Return_statements">return statement</a>,
+deferred functions are executed <i>after</i> any result parameters are set
+by that return statement but <i>before</i> the function returns to its caller.
If a deferred function value evaluates
to <code>nil</code>, execution <a href="#Handling_panics">panics</a>
when the function is invoked, not when the "defer" statement is executed.
@@ -5572,12 +5575,13 @@ for i := 0; i &lt;= 3; i++ {
defer fmt.Print(i)
}
-// f returns 1
+// f returns 42
func f() (result int) {
defer func() {
- result++
+ // result is accessed after it was set to 6 by the return statement
+ result *= 7
}()
- return 0
+ return 6
}
</pre>