diff --git a/examples/issue-loop-struct-slice/issue-loop-struct-slice.go b/examples/issue-loop-struct-slice/issue-loop-struct-slice.go index f8026e8..ffdb01d 100644 --- a/examples/issue-loop-struct-slice/issue-loop-struct-slice.go +++ b/examples/issue-loop-struct-slice/issue-loop-struct-slice.go @@ -1,8 +1,14 @@ -// interview questions: +// Challenge: // // What is expected output? // // How to fix this code? +// +// Reference: [Go Wiki: Range Clauses](https://go.dev/wiki/Range) +// +// Reference: [Should you use slices of pointers to structs?](https://www.willem.dev/articles/slice-of-pointers-structs/) +// +// Reference: [Go Spec: For statements](https://go.dev/ref/spec#For_statements) package main @@ -26,19 +32,23 @@ func main() { fmt.Printf("player %s has score %d\n", p.name, p.score) } - // (End of Question) + // (End of Challenge) // ---------- // Explaination: - // The player struct is stored in the backing array of the slice players // - // `p` is actually only a copy of the original player + // - The player struct is stored in the backing array of the slice players + // + // - p is declaredin in range clause using a form of [short variable declaration (:=)](https://go.dev/ref/spec#Short_variable_declarations). The variables have the types of their respective iteration values. + // + // - `p` is only a copy of the original player + // + // - when updating `p.score`, it only updates the "copy" of the player in the slice for _, p := range players { p.score += 10 } // How to fix it? - fmt.Println("---- After fix ------") // Directly access the player in the slice for i := range players { @@ -48,12 +58,4 @@ func main() { for _, p := range players { fmt.Printf("player %s has score %d\n", p.name, p.score) } - - // Output: - // - // player Bob has score 20 - // - // player Alice has score 217 } - -// Reference: [Should you use slices of pointers to structs?](https://www.willem.dev/articles/slice-of-pointers-structs/) diff --git a/examples/issue-loop-struct-slice/issue-loop-struct-slice.hash b/examples/issue-loop-struct-slice/issue-loop-struct-slice.hash index 4cbf37c..646f5a4 100644 --- a/examples/issue-loop-struct-slice/issue-loop-struct-slice.hash +++ b/examples/issue-loop-struct-slice/issue-loop-struct-slice.hash @@ -1,2 +1,2 @@ -1acf36d67df9c219102a45e722d1cbb38dbd8ce5 -b5YAS_z1uop +7d7891806eda3e24148d21ce6a31e2fbaf3ea623 +gb9N6mi5JjT diff --git a/public/issue-loop-struct-slice b/public/issue-loop-struct-slice index 0d43f6a..4b313ca 100644 --- a/public/issue-loop-struct-slice +++ b/public/issue-loop-struct-slice @@ -27,12 +27,18 @@
interview questions:
+Challenge:
What is expected output?
How to fix this code?
+Reference: Go Wiki: Range Clauses
+ +Reference: Should you use slices of pointers to structs?
+ +Reference: Go Spec: For statements
+package main
(End of Question)
+(End of Challenge)
Explaination: -The player struct is stored in the backing array of the slice players
+Explaination:
+ +The player struct is stored in the backing array of the slice players
p is declaredin in range clause using a form of short variable declaration (:=). The variables have the types of their respective iteration values.
p
is only a copy of the original player
p
is actually only a copy of the original player
when updating p.score
, it only updates the “copy” of the player in the slice
How to fix it?
-Directly access the player in the slice
-fmt.Println("---- After fix ------") ++ fmt.Println("---- After fix ------")
Directly access the player in the slice
+for i := range players { ++ for i := range players { players[i].score += 10 }@@ -178,43 +183,16 @@ The player struct is stored in the backing array of the slice players- + - for _, p := range players { fmt.Printf("player %s has score %d\n", p.name, p.score) } --
Output:
- -player Bob has score 20
- -player Alice has score 217
- -
}