切片赋给指针p,为什么不能用p[0]取值,数组却可以?

taatcc · · 1045 次点击
可是指向切片就不能用p[0],是什么原因啊,而数组即可以用p[0],也可以用(*p)[0],有点想不通
#2
更多评论
p是一个指向切片的指针,你可以(*p)[0]
#1
<a href="/user/taatcc" title="@taatcc">@taatcc</a> 参见这个:http://docs.studygolang.com/ref/spec#Index_expressions A primary expression of the form a[x] denotes the element of the array, pointer to array, slice, string or map a indexed by x. The value x is called the index or map key, respectively. ...... For a of pointer to array type: a[x] is shorthand for (*a)[x] ...... Otherwise a[x] is illegal. 索引表达式只支持这几种类型,其它的都是非法的。 你要对指向切片的指针使用索引表达式,就需要手动(*p)[0]
#3