Skip to content
Permalink
Browse files

corrent explanations for The Evaluation Moment of Deferred Function V…

…alues
  • Loading branch information...
go101 committed Oct 30, 2019
1 parent 35b8f5e commit 8b62837d3c957f45664679a622872d292806f76a
Showing with 49 additions and 9 deletions.
  1. +6 −3 UPDATES.md
  2. +14 −1 articles/101.html
  3. +1 −0 articles/acknowledgements.html
  4. +28 −5 articles/defer-more.html
@@ -1,9 +1,12 @@
### 1.13.g (2019/Oct/30)

* fix a bug in the example code in the [Delete a segment of slice elements](https://gfw.go101.org/article/container.html#delete-slice-elements) section

### 1.13.i (2019/Oct/30)

* fix a bug in the example code in the [Delete a segment of slice elements](https://go101.org/article/container.html#delete-slice-elements) section
of the "Arrays, Slices and Maps in Go" article.
* correct explainations in [The Evaluation Moment of Deferred Function Values](https://go101.org/article/function.html#function-evaluation-time).

### 1.13.f (2019/Oct/18)
### 1.13.h (2019/Oct/18)

* correct the explanations for the [Evaluation and Assignment Orders in Assignment Statements](https://go101.org/article/evaluation-orders.html#value-assignment) section
in the "Expression Evaluation Orders" article.
@@ -73,6 +73,19 @@



<div class="alert alert-warning"><small>
<i>2019/Oct/30</i>:
<ul>
<li>
<a href="https://github.com/go101/go101/commit/35b8f5e2aa7440c25bf0a784b5bde506d4ec3a65#diff-8eb02937949dd308587a57ed66732712">fixed a bug</a> in the "deleting subslice" example.
</li>
<li>
<a href="https://github.com/golang101/golang101/commit/facaf82943a5c0d21bec96ce76ad70d60fccdda7">fixed a bug</a>
</li>
</ul>

</small></div>

<div class="alert alert-warning"><small>
<i>2019/Oct/18</i>: <a href="https://github.com/golang/go/issues/27821#issuecomment-543611217">An explanation description flaw</a> is found in the
<a href="evaluation-orders.html#value-assignment">Evaluation and Assignment Orders in Assignment Statements</a> section
@@ -93,12 +106,12 @@
in the "The Right Places to Call the recover Function" article. The section containing the mistake will be modified after some time.
</small></div>

<!--
<div class="alert alert-warning"><small>
<i>2019/Sep/19</i>: The "named type" and "unnamed type" terminologies are added back in this book.
But they are eqivalent to "defined type" and "non-defined type" now.
</small></div>
<!--
<div class="alert alert-warning"><small>
NOTE: if your last <code>go get -u</code> command run was at before <b><i>July 28th, 2018</i></b>,
please run this command again to install the latest <code>go101</code> program and articles.
@@ -138,6 +138,7 @@ <h1>Acknowledgements</h1>
Tony Bai,
lanastasov,
liukaifei,
Abirdcfly,
etc.
</p>

@@ -43,12 +43,35 @@ <h3>Calls to Many Built-in Functions With Return Results Can't Be Deferred</h3>
</code></pre>
</div>

<a class="anchor" id="function-evaluation-time"></a>
<h3>The Evaluation Moment of Deferred Function Values</h3>

<div>
The called function (value) in a deferred function call is evaluated
when the call is pushed into the deferred call stack of the current goroutine.
For example, the following program will print <code>false</code>.

<pre class="line-numbers"><code class="language-go">package main

import "fmt"

func main() {
var f = func () {
fmt.Println(false)
}
defer f()
f = func () {
fmt.Println(true)
}
}
</code></pre>

<p></p>

The called function in a deferred function call may be a nil function value.
For such a case, the panic will occur before the deferred call is prepared to
be pushed into the deferred call stack of the current goroutine. An example:
For such a case, the panic will occur when the call to the nil function is invoked,
instead of when the call is pushed into the deferred call stack of the current goroutine.
An example:
<pre class="line-numbers"><code class="language-go">package main

import "fmt"
@@ -57,9 +80,9 @@ <h3>The Evaluation Moment of Deferred Function Values</h3>
defer fmt.Println("reachable")
var f func() // f is nil by default
defer f() // panic here
// The following lines are dead code.
fmt.Println("not reachable")
f = func() {}
// The following lines are also reachable.
fmt.Println("also reachable")
f = func() {} // useless to avoid panicking
}
</code></pre>

0 comments on commit 8b62837

Please sign in to comment.
You can’t perform that action at this time.