From 4a903e0b32be5a590880ceb7379e68790602c29d Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 27 Jan 2009 09:29:40 -0800 Subject: [PATCH] defer statement R=r DELTA=30 (26 added, 0 deleted, 4 changed) OCL=23533 CL=23569 --- doc/go_spec.txt | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/doc/go_spec.txt b/doc/go_spec.txt index 9a10a5435d..b5dd2b4621 100644 --- a/doc/go_spec.txt +++ b/doc/go_spec.txt @@ -3,7 +3,7 @@ The Go Programming Language Specification (DRAFT) Robert Griesemer, Rob Pike, Ken Thompson -(January 23, 2009) +(January 26, 2009) ---- @@ -235,6 +235,7 @@ Contents Continue statements Label declaration Goto statements + Defer statements Function declarations Method declarations @@ -682,8 +683,8 @@ Reserved words The following words are reserved and must not be used as identifiers: break default func interface select - case else go map struct - chan goto package switch + case defer go map struct + chan else goto package switch const fallthrough if range type continue for import return var @@ -2501,7 +2502,8 @@ Statements control execution. Statement = Declaration | LabelDecl | EmptyStat | SimpleStat | GoStat | ReturnStat | BreakStat | ContinueStat | GotoStat | - FallthroughStat | Block | IfStat | SwitchStat | SelectStat | ForStat . + FallthroughStat | Block | IfStat | SwitchStat | SelectStat | ForStat | + DeferStat . SimpleStat = ExpressionStat | IncDecStat | Assignment | SimpleVarDecl . @@ -3015,6 +3017,30 @@ clause of the switch statement. FallthroughStat = "fallthrough" . +Defer statements +---- + +A defer statement invokes a function whose execution is deferred to the moment +when the surrounding function returns. + + DeferStat = "defer" Expression . + +The expression must be a function call. Each time the defer statement executes, +the parameters to the function call are evaluated and saved anew but the +function is not invoked. Immediately before the innermost function surrounding +the defer statement returns, but after its return value (if any) is evaluated, +each deferred function is executed with its saved parameters. Deferred functions +are executed in LIFO order. + + lock(l); + defer unlock(l); // unlocking happens before surrounding function returns + + // prints 3 2 1 0 before surrounding function returns + for i := 0; i <= 3; i++ { + defer print(i); + } + + ---- Function declarations -- 2.48.1