From 888dbf4beb6842afaabf2b6260ddcb0ac967ec0b Mon Sep 17 00:00:00 2001 From: Go101 <22589241+go101@users.noreply.github.com> Date: Sat, 11 May 2024 23:38:22 +0800 Subject: [PATCH] fix inaccuracy about interface embed types --- ...1-for-loop-semantic-changes-in-go-1.22.tmd | 15 ---------- pages/fundamentals/acknowledgements.html | 2 ++ pages/fundamentals/type-embedding.html | 5 ++-- pages/fundamentals/unofficial-faq.html | 29 +++++++++++++++---- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/pages/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.tmd b/pages/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.tmd index 808c6e99..66ec5146 100644 --- a/pages/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.tmd +++ b/pages/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.tmd @@ -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, @@ -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 @@ -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. @@ -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: @@ -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: @@ -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 @@ -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 @@ -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, @@ -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. @@ -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 @@ -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 @@ -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 @@ -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, @@ -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 @@ -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 diff --git a/pages/fundamentals/acknowledgements.html b/pages/fundamentals/acknowledgements.html index e08a21aa..dfcc0ac6 100644 --- a/pages/fundamentals/acknowledgements.html +++ b/pages/fundamentals/acknowledgements.html @@ -216,6 +216,8 @@
-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 interfaces in Go for details.
diff --git a/pages/fundamentals/unofficial-faq.html b/pages/fundamentals/unofficial-faq.html index b85ddd69..7ed0fff4 100644 --- a/pages/fundamentals/unofficial-faq.html +++ b/pages/fundamentals/unofficial-faq.html @@ -166,6 +166,9 @@+
+declared and not used
mean?
@@ -336,6 +339,9 @@ +
+new(T)
a sugar of var t T; (&t)
?
@@ -640,6 +646,9 @@ +
+time.Sleep(d)
and the channel receive operation <-time.After(d)
?
@@ -714,6 +723,9 @@ +
+fmt.Print
and fmt.Println
functions?
@@ -940,6 +952,9 @@ +
+[]T1
and []T2
share the same underlying type
@@ -1002,6 +1017,8 @@ +
set
container type?
Sets are just maps but don't care about element values.
In Go, map[Tkey]struct{}
is often used as a set type.
-
Please read atomic operations for pointers. -
iota
mean?
Iota is the ninth letter of the Greek alphabet.
In Go, iota
is used in constant declarations.
In each constant declaration group, its value is N
in the Nth constant specification in that constant declaration group.
This allows for easy declaration of related constants.
-