I read this article and decided to repeat such behavior myself and experiment with that:
package main
import (
"fmt"
"time"
)
type User struct {
i int
token string
}
func NewUser(i int, token string) User {
user := User{token: fmt.Sprint(i), i: i}
return user
}
func (u *User) PrintAddr() {
fmt.Printf("%d (PrintAddr): %p
", u.i, u)
}
func main() {
users := make([]User, 4)
for i := 0; i < 4; i++ {
user := NewUser(i, "")
users[i] = user
}
for i, user := range users {
go user.PrintAddr()
go users[i].PrintAddr()
}
time.Sleep(time.Second)
}
(Playground)
Here is the code output:
1 (PrintAddr): 0xc000056198
2 (PrintAddr): 0xc0000561b0
0 (PrintAddr): 0xc000056180
3 (PrintAddr): 0xc00000c030
3 (PrintAddr): 0xc00000c030
3 (PrintAddr): 0xc00000c030
3 (PrintAddr): 0xc00000c030
3 (PrintAddr): 0xc0000561c8
I also don't understand, why are 4 of 5 3 (PrintAddr)
are 0xc00000c030
, and the last one is different?
However, if I use a pointer array instead of value array, like this,
func NewUser(i int, token string) *User {
user := &User{token: fmt.Sprint(i), i: i}
return user
}
// -snip-
func main() {
users := make([]*User, 4)
// -snip-
(Playground)
then everything's fine here and each entry is printed exactly 2 times with the same address:
1 (PrintAddr): 0xc0000ae030
3 (PrintAddr): 0xc0000ae060
2 (PrintAddr): 0xc0000ae048
2 (PrintAddr): 0xc0000ae048
3 (PrintAddr): 0xc0000ae060
1 (PrintAddr): 0xc0000ae030
0 (PrintAddr): 0xc0000ae018
0 (PrintAddr): 0xc0000ae018
But why did the situation in the article not apply here and I didn't get many 3 (PrintAddr)
instead?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…