go中* [] Type和[] * Type有什么区别

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?
}
  1. What's exactly is the meaning of []*Person and *[]Person?

  2. 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.