2

Is there a short hand for accessing x.Bar[0] in the following case?

The direct attempt results in (type *[]string does not support indexing) for obvious reasons

type A struct {
    B *[]string
}


x := &Foo{Bar: &[]string{"1", "2"}}
salient
  • 2,316
  • 6
  • 28
  • 43
  • 3
    What is the reason to have a pointer to a slice? PS: `(*x.Bar)[0]` – zerkms Jul 19 '17 at 20:10
  • I have abstracted some more complex protobuf stuff :) – salient Jul 19 '17 at 20:11
  • Related / possible duplicate of: [Slicing a slice pointer passed as argument](https://stackoverflow.com/questions/38013922/slicing-a-slice-pointer-passed-as-argument/38014097#38014097). – icza Jul 19 '17 at 21:19

1 Answers1

8

It would be

(*x.Bar)[0]

You use parentheses to change the precedence of operators: [] has a higher precedence than *.

zerkms
  • 249,484
  • 69
  • 436
  • 539