1
在以下情况下是否存在访问x.Bar[0]
的简写?当切片为指针时按索引访问切片
直接尝试导致(type *[]string does not support indexing)
原因很明显
type A struct {
B *[]string
}
x := &Foo{Bar: &[]string{"1", "2"}}
在以下情况下是否存在访问x.Bar[0]
的简写?当切片为指针时按索引访问切片
直接尝试导致(type *[]string does not support indexing)
原因很明显
type A struct {
B *[]string
}
x := &Foo{Bar: &[]string{"1", "2"}}
这将是
(*x.Bar)[0]
您使用括号改变运算符的优先级:[]
比*
更高的优先级。
有一个指向切片的指针的原因是什么? PS:'(* x.Bar)[0]' – zerkms
我已经提取了一些更复杂的protobuf东西:) – salient
相关/可能重复:[切片作为参数传递的切片指针](https://stackoverflow.com/questions/38013922 /切片-A-切片指针传入作为参数的/ 38014097#38014097)。 – icza