Hi,
I am trying to understand what would be the best way to go when it comes to using a type User
that contains slice of other types Comment
or others. Each example have their own questions below.
Thanks
type User struct {
Name string
Age int
Salary float64
Username string
Password string
Comments []Comment // Or *Comment
// SomeOtherSlices []Other
}
type Comment struct {
ID string
Message string
CreatedAt time.Time
DeletedAt *time.Time
}
// Would inner `Comments` be copied everytime I passed returned value around?
func storageV1() *User {
return &User{
// ...
Comments: []Comment{{
// ..
}},
}
}
// Given the returned value is a copy and the inner `Comments` a pointer, would inner `Comments` be copied everytime I passed returned value around?
func storageV2() User {
return User{
// ...
Comments: []*Comment{{ // If the field was a pointer
// ..
}},
}
}
// I am not sure if both have to be pointer?
func storageV3() *User {
return &User{
// ...
Comments: []*Comment{{ // If the field was a pointer
// ..
}},
}
}