Let's say we have a struct named Person
and it's held by a struct named People
.
type Person {
Name string
age int
}
type People {
City string
List []*Person //check this out
}
type People2 {
City string
List *[]Person //What's the difference?
}
What's exactly is the meaning of []*Person
and *[]Person
?
How can I retrieve the element's value of these slice?
I'm more familiar with C so it's appreciated if you can explain this in C
[]*Type
is a slice of pointers to Type
.*[]Type
is a pointer to a slice of Type
.To deference an individual element from a slice of pointers:
var x []*Type
y := *x[0]
To dereference an individual element from a pointer to a slice:
var x *[]Type
y := (*x)[0]
And for bonus points, to deference an individual element from a pointer to a slice of pointers:
var x *[]*Type
y := *(*x)[0]
See playground.