From 1e8e14c901cebde0550c4fe0c1a77b3902d6080d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 1 Nov 2012 10:13:48 -0700 Subject: [PATCH] spec: clarify returns, defer statements, and panics This is an attempt at making the interaction between these three constructs clearer. Specifically: - return statements terminate a function, execute deferred functions, return to the caller, and then execution continues after the call - panic calls terminate a function, execute deferred functions, return to the caller, and then re-panic - deferred functions are executed before a function _returns_ to its caller The hope is that with this change it becomes clear when a deferred function is executed (when a function returns), and when it is not (when a program exits). R=r, rsc, iant, ken, iant CC=golang-dev https://golang.org/cl/6736071 --- doc/go_spec.html | 63 ++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 0f3c5ed312..c5b1c42629 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -4540,8 +4540,10 @@ select {} // block forever

Return statements

-A "return" statement terminates execution of the containing function -and optionally provides a result value or values to the caller. +A "return" statement in a function F terminates the execution +of F, and optionally provides one or more result values. +Any functions deferred by F +are executed before F returns to its caller.

@@ -4611,7 +4613,10 @@ func (devnull) Write(p []byte) (n int, _ error) {
 
 
 

-Regardless of how they are declared, all the result values are initialized to the zero values for their type (§The zero value) upon entry to the function. +Regardless of how they are declared, all the result values are initialized to the zero +values for their type (§The zero value) upon entry to the +function. A "return" statement that specifies results sets the result parameters before +any deferred functions are executed.