Pointers

Fill in the blanks in line A and B, to assure the printed output is "foo"

  1. package main
  2. type S struct {
  3. m string
  4. }
  5. func f() *S {
  6. return __ //A
  7. }
  8. func main() {
  9. p := __ //B
  10. print(p.m) //print "foo"
  11. }

Answer

  1. package main
  2. type S struct {
  3. m string
  4. }
  5. func f() *S {
  6. return &S{"foo"} //A
  7. }
  8. func main() {
  9. p := *f() //B
  10. print(p.m) //print "foo"
  11. }