Skip to content

Commit 18481eb

Browse files
authored
feat: Issue: Loop Struct Slice (#31)
1 parent 0e60bca commit 18481eb

11 files changed

+589
-6
lines changed

examples.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Methods | methods
1111
Channel: Merge | channel-merge
1212
Reflection | reflection
1313
Issue: Goroutine Leak | issue-goroutine-leak
14+
Issue: Loop Struct Slice | issue-loop-struct-slice
1415
encoding/json | encoding-json
1516
Url Parsing | url-parsing
1617
http Request | http-req
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// interview questions:
2+
//
3+
// What is expected output?
4+
//
5+
// How to fix this code?
6+
7+
package main
8+
9+
import "fmt"
10+
11+
func main() {
12+
type player struct {
13+
name string
14+
score int
15+
}
16+
players := []player{
17+
{"Bob", 10},
18+
{"Alice", 207},
19+
}
20+
21+
for _, p := range players {
22+
p.score += 10
23+
}
24+
25+
for _, p := range players {
26+
fmt.Printf("player %s has score %d\n", p.name, p.score)
27+
}
28+
29+
// (End of Question)
30+
// ----------
31+
32+
// Explaination:
33+
// The player struct is stored in the backing array of the slice players
34+
//
35+
// `p` is actually only a copy of the original player
36+
for _, p := range players {
37+
p.score += 10
38+
}
39+
40+
// How to fix it?
41+
42+
fmt.Println("---- After fix ------")
43+
// Directly access the player in the slice
44+
for i := range players {
45+
players[i].score += 10
46+
}
47+
48+
for _, p := range players {
49+
fmt.Printf("player %s has score %d\n", p.name, p.score)
50+
}
51+
52+
// Output:
53+
//
54+
// player Bob has score 20
55+
//
56+
// player Alice has score 217
57+
}
58+
59+
// Reference: [Should you use slices of pointers to structs?](https://www.willem.dev/articles/slice-of-pointers-structs/)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1acf36d67df9c219102a45e722d1cbb38dbd8ce5
2+
b5YAS_z1uop
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
player Bob has score 10
2+
player Alice has score 207
3+
---- After fix ------
4+
player Bob has score 20
5+
player Alice has score 217

public/encoding-json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/encoding-json.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.html

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/issue-goroutine-leak

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/issue-goroutine-leak.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/issue-loop-struct-slice

Lines changed: 257 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)