title: How to set the composite field of a struct field by name in Go?
body: <p>I have a struct with one of it's fields being another struct and I would like to access this struct by name (as a parameter). I followed <a href="https://stackoverflow.com/questions/6395076/using-reflect-how-do-you-set-the-value-of-a-struct-field">Using reflect, how do you set the value of a struct field?</a> and it works for basic type but not for composite types. </p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "reflect"
  )
  
  
  type PntInt struct {
      p *int64
  }
  
  type Foo struct {
      X  int64
      Px PntInt
  }
  
  func main() {
      foo := Foo{}
      fmt.Println(foo)
      i := int64(8)
      Pi := PntInt{&amp;i}
      reflect.ValueOf(&amp;foo).Elem().FieldByName("X").SetInt(i)
      reflect.ValueOf(&amp;foo).Elem().FieldByName("Px").Set(Pi)
      fmt.Println(foo)
  }
  </code></pre>
  
  <p>setting the integer works but trying to set "Px" fails with the error</p>
  
  <pre><code>./prog.go:25:52: cannot use Pi (type PntInt) as type reflect.Value in argument to reflect.ValueOf(&amp;foo).Elem().FieldByName("Px").Set
  </code></pre>