Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from go101:master #39

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions pages/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.tmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

###############################################################
# `for` Loop Semantic Changes in Go 1.22: Be Aware of the Impact

Go 1.22 changed the semantics of `for` loops,
Expand All @@ -12,7 +11,6 @@ understand the implications of these changes
in order to write Go code which will behave as intended.
Otherwise, your code may exhibit unexpected behavior.

################################################
## What are the changes?

Specifically speaking, only the semantics of the `for` loops
Expand Down Expand Up @@ -166,7 +164,6 @@ see

@__the Go 1 promise of compatibility `` https://go.dev/doc/go1compat

###########################################
## The impact of the changes

Personally, I think the rationale of the change to `for-range` loops is well-justified.
Expand Down Expand Up @@ -236,7 +233,6 @@ For a language that promotes explicitness, it's embarrassing.
Implicitness often leads to unexpected surprises, which is not a surprise.
The following will show several examples which might break your expectations.

#####################################################################################
### The behaviors of deferred function calls which capture loop variables might change

A simple example:
Expand Down Expand Up @@ -366,7 +362,6 @@ for counter, n := 0, 2; n >= 0; n := n - 1 { ... }
However, sadly, the suggestion was ignored totally.
}

#######################################################
### Be careful when capturing loop variables in closures

An example:
Expand Down Expand Up @@ -464,7 +459,6 @@ 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 +551,6 @@ $ 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 +609,6 @@ 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 Expand Up @@ -766,7 +758,6 @@ This is just a suggestion, not a mandatory rule,
because copying no-copy values does not always cause damage
(but the damage may be exposed later when the code is refactored in some way).

#################################################################
## Warning: the performance of your Go programs might be degraded silently

Sometimes, a compiler is over smart; sometimes, it is not smart enough.
Expand Down Expand Up @@ -845,7 +836,6 @@ Suggestions to avoid such performance degradation issue:
the loop itself to optimize performance. This is beneficial if you can
guarantee that the variables don't need to be instantiated in each iteration.

#########################################################################
### Warning: things might become more subtle than before
when loop variables are used concurrently

Expand Down Expand Up @@ -1040,7 +1030,6 @@ We can still use the old trick: just add an `i := i` line at the start of the lo
Yes, this is still the best suggestion to avoid data race for such situations in the Go 1.22+ era.
Is this a mockery of the new semantics (of `for;;` loops)?

################################
## Advice and suggestions

Okay, the above are the potential issues I've identified so far
Expand All @@ -1049,7 +1038,6 @@ There might be more, I'm not sure.

Here are some recommendations you can follow in the Go 1.22+ era.

#######################################################
### Specify Go language versions for Go source files

As demonstrated in many above examples, the semantic changes made
Expand Down Expand Up @@ -1098,7 +1086,6 @@ Anyway, since Go 1.22, you should try to specify a Go language version for every
in any of the above introduced ways, to avoid compiler version dependent behaviors.
This is the minimum standard to be a professional Go programmer in the Go 1.22+ era.

###############################################
### Upgrading module versions

If you are maintaining a public Go module which are depended by other Go projects,
Expand All @@ -1112,7 +1099,6 @@ pay attention to those ones which language versions os
upgraded to Go 1.22 or higher
from a version with the old semantics before Go 1.22.

#########################################################
### Avoid using freshly-declared loop variables in `for;;` loops
if you worry about getting bitten by the pitful of the new semantics

Expand Down Expand Up @@ -1157,7 +1143,6 @@ only instantiated once during the whole loop.

This is a little awkward, but it is much safer.

#################################
## Final words

Overall, I find the impact of the new semantics of `for-range` loops
Expand Down
2 changes: 2 additions & 0 deletions pages/fundamentals/acknowledgements.html
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ <h1>Acknowledgments</h1>
DrejT,
Wildan S. Nahar,
Rahul,
zlqpzww,
iridescentGray,
etc.
</p>

Expand Down
5 changes: 2 additions & 3 deletions pages/fundamentals/type-embedding.html
Original file line number Diff line number Diff line change
Expand Up @@ -898,11 +898,10 @@ <h3>Normalization and Evaluation of Promoted Method Values</h3>

</div>

<h3>Interface Types Embed Interface Types</h3>
<h3>Interface Types Embed All Kinds of Types</h3>

<p>
Not only can struct types embed other types, but also can interface types,
but interface types can only embed interface types.
Interface types can embed all kinds of types.
Please read <a href="interface.html">interfaces in Go</a> for details.
</p>

Expand Down
29 changes: 23 additions & 6 deletions pages/fundamentals/unofficial-faq.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ <h3>

</div>

<p>
</p>

<a class="anchor" id="error-declared-not-used"></a>
<h3>
What does the compiler error message <code>declared and not used</code> mean?
Expand Down Expand Up @@ -336,6 +339,9 @@ <h3>
</code></pre>
</div>

<p>
</p>

<a class="anchor" id="new-function"></a>
<h3>
Is <code>new(T)</code> a sugar of <code>var t T; (&amp;t)</code>?
Expand Down Expand Up @@ -640,6 +646,9 @@ <h3>
</code></pre>
</div>

<p>
</p>

<a class="anchor" id="time-sleep-after"></a>
<h3>
What is the difference between the function call <code>time.Sleep(d)</code> and the channel receive operation <code>&lt;-time.After(d)</code>?
Expand Down Expand Up @@ -714,6 +723,9 @@ <h3>
</code></pre>
</div>

<p>
</p>

<a class="anchor" id="fmt-print-println"></a>
<h3>
What are the differences between the <code>fmt.Print</code> and <code>fmt.Println</code> functions?
Expand Down Expand Up @@ -940,6 +952,9 @@ <h3>
</code></pre>
</div>

<p>
</p>

<a class="anchor" id="slice-conversion"></a>
<h3>
Why don't type <code>[]T1</code> and <code>[]T2</code> share the same underlying type
Expand Down Expand Up @@ -1002,6 +1017,8 @@ <h3>
}
</code></pre>
</div>
<p>
</p>

<a class="anchor" id="unaddressable-values"></a>
<h3>
Expand Down Expand Up @@ -1228,10 +1245,10 @@ <h3>
Why isn't there a built-in <code>set</code> container type?
</h3>

<div>
<p>
Sets are just maps but don't care about element values.
In Go, <code>map[Tkey]struct{}</code> is often used as a set type.
</div>
</p>

<a class="anchor" id="byte-rune-slice-string"></a>
<h3>
Expand Down Expand Up @@ -1281,9 +1298,9 @@ <h3>
How to manipulate pointer values atomically?
</h3>

<div>
<p>
Please read <a href="concurrent-atomic-operation.html#pointer">atomic operations for pointers</a>.
</div>
</p>

<!-- others -->

Expand All @@ -1292,13 +1309,13 @@ <h3>
What does <code>iota</code> mean?
</h3>

<div>
<p>
Iota is the ninth letter of the Greek alphabet.
In Go, <code>iota</code> is used in constant declarations.
In each constant declaration group, its value is <code>N</code>
in the <b><i>N</i></b>th constant specification in that constant declaration group.
This allows for easy <a href="https://golang.org/ref/spec#Iota">declaration of related constants</a>.
</div>
</p>

<a class="anchor" id="check-if-channels-closed"></a>
<h3>
Expand Down
Loading