Skip to content

Commit

Permalink
fix a blog tmd
Browse files Browse the repository at this point in the history
  • Loading branch information
zigo101 committed Jun 7, 2024
1 parent 6ca9d27 commit eae17c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions pages/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.html
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ <h3>Be careful when capturing loop variables in closures</h3>
</div>

<p></p>
#:::::::::::::::::::::::::::::::::::::::::::::::::; Be careful when taking addresses of loop variables

<h3>Be careful when taking addresses of loop variables</h3>

<p></p>
Similarly, since Go 1.22, it may be dangerous to use the address of a freshly-declared loop variable across loop iterations.
<p></p>
Expand Down Expand Up @@ -510,7 +512,9 @@ <h3>Be careful when capturing loop variables in closures</h3>
</pre>

<p></p>
#::::::::::::::::::::::::::::::::::::::::::::::::; Be careful when moving the 3rd clause statements inside loop bodies

<h3>Be careful when moving the 3rd clause statements inside loop bodies</h3>

<p></p>
Since Go 1.22, the following two loops might be not equivalent with each other any more (prior to Go 1.22, they are equivalent).
<p></p>
Expand Down Expand Up @@ -565,7 +569,9 @@ <h3>Be careful when capturing loop variables in closures</h3>
</pre>

<p></p>
#::::::::::::::::::::::::::::::::::::::::::::::::::; Be careful when declaring no-copy values as loop variables

<h3>Be careful when declaring no-copy values as loop variables</h3>

<p></p>
As explained above, since Go 1.22, at the start of each loop iteration, each freshly-declared loop variable will get copied once, <span class='tmd-bold'><span class='tmd-italic'>implicitly</span></span>. The implication means that, since Go 1.22, it is not a good idea to declare no-copy values as loop variables, such as <code class='tmd-code-span'>sync.Mutex</code>, <code class='tmd-code-span'>sync/atomic.Int64</code>, <code class='tmd-code-span'>bytes.Buffer</code>, and <code class='tmd-code-span'>strings.Builder</code> values etc.
<p></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ since Go 1.22, a freshly-declared loop variable may have many instances at run t
whether or not it is modified in `postStatement`.
Each of the instances is instantiated in one iteration.

#:::::::::::::::::::::::::::::::::::::::::::::::::;
#::::::::::::::::::::::::::::::::::::::::::::::::
Be careful when taking addresses of loop variables

Similarly, since Go 1.22, it may be dangerous to use
Expand Down Expand Up @@ -557,7 +557,7 @@ $ gotv 1.22. run demo-pointer2.go
...
'''

#::::::::::::::::::::::::::::::::::::::::::::::::;
#::::::::::::::::::::::::::::::::::::::::::::::::
Be careful when moving the 3rd clause statements inside loop bodies

Since Go 1.22, the following two loops might be not equivalent with each other any more
Expand Down Expand Up @@ -616,7 +616,7 @@ true
2
'''

#::::::::::::::::::::::::::::::::::::::::::::::::::;
#::::::::::::::::::::::::::::::::::::::::::::::::::
Be careful when declaring no-copy values as loop variables

As explained above, since Go 1.22, at the start of each loop iteration,
Expand Down

0 comments on commit eae17c3

Please sign in to comment.