title: Get pointer to value using reflection
body: <p>I have a function that iterates through all fields of an interface passed as parameter. In order to achieve this is I am using reflection. The issue is that I do not know how to obtain the address of a non-pointer field. Here is an example:</p>
  
  <pre><code>type Z struct {
      Id int
  }
  
  type V struct {
      Id int
      F Z
  }
  
  type T struct {
      Id int
      F V
  }
  </code></pre>
  
  <p>The above code represents my test structures. Now here is the actual function which traverses a specified structure and lists details about it:</p>
  
  <pre><code>func InspectStruct(o interface{}) {
       val := reflect.ValueOf(o)
       if val.Kind() == reflect.Interface &amp;&amp; !val.IsNil() {
          elm := val.Elem()
          if elm.Kind() == reflect.Ptr &amp;&amp; !elm.IsNil() &amp;&amp; elm.Elem().Kind() == reflect.Ptr {
              val = elm
          }
       }
       if val.Kind() == reflect.Ptr {
          val = val.Elem()
       }
  
      for i := 0; i &lt; val.NumField(); i++ {
          valueField := val.Field(i)
          typeField := val.Type().Field(i)
          address := "not-addressable"
  
          if valueField.Kind() == reflect.Interface &amp;&amp; !valueField.IsNil() {
              elm := valueField.Elem()
              if elm.Kind() == reflect.Ptr &amp;&amp; !elm.IsNil() &amp;&amp; elm.Elem().Kind() == reflect.Ptr {
                  valueField = elm
              }
          }
          if valueField.Kind() == reflect.Ptr {
              valueField = valueField.Elem()
          }
          if valueField.CanAddr() {
              address = fmt.Sprint(valueField.Addr().Pointer())
          }
  
          fmt.Printf("Field Name: %s,\t Field Value: %v,\t Address: %v\t, Field type: %v\t, Field kind: %v\n", typeField.Name, 
              valueField.Interface(), address, typeField.Type, valueField.Kind())
  
          if valueField.Kind() == reflect.Struct {
              InspectStruct(valueField.Interface())
          }
      }
  }
  </code></pre>
  
  <p>And here is the actual test after structure instantiation/initialization:</p>
  
  <pre><code>t := new(T)
  t.Id = 1
  t.F = *new(V)
  t.F.Id = 2
  t.F.F = *new(Z)
  t.F.F.Id = 3
  
  InspectStruct(t)
  </code></pre>
  
  <p>And finally the output of InspectStruct call:</p>
  
  <pre><code>Field Name: Id,  Field Value: 1,     Address: 408125440 , Field type: int   , Field kind: int
  Field Name: F,   Field Value: {2 {3}},   Address: 408125444 , Field type: main.V    , Field kind: struct
  Field Name: Id,  Field Value: 2,     Address: not-addressable   , Field type: int   , Field kind: int
  Field Name: F,   Field Value: {3},   Address: not-addressable   , Field type: main.Z    , Field kind: struct
  Field Name: Id,  Field Value: 3,     Address: not-addressable   , Field type: int   , Field kind: int
  </code></pre>
  
  <p>As you can see I am using recursion, so if one of the fields is a struct kind then I call InspectStruct for it.
  <strong>My issue is that though all fields have been initialized for the entire structure "t" hierarchy, I am not able to get the address for any field located at a higher depth than "t". I would really appreciate any help.</strong></p>