假设我们有一个名为 Person
的结构,它由一个名为 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?
}
[]*Person
和*[]Person
到底是什么意思?如何获取这些 slice 的元素值?
我比较熟悉 C,所以如果你能用 C 解释一下,我将不胜感激
最佳答案
[]*Type
是指向Type
的指针片段。*[]Type
是指向Type
slice 的指针。
从指针 slice 中引用单个元素:
var x []*Type
y := *x[0]
从指向 slice 的指针中解引用单个元素:
var x *[]Type
y := (*x)[0]
为了加分,将单个元素从指针推导到指针 slice :
var x *[]*Type
y := *(*x)[0]
参见 playground .
关于go - *[]Type 和 []*Type 在 go 中有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50201366/