title: How to use reflect to recursively parse nested struct in Go?
body: <p>I have a nested three layer struct.
  I would like to use reflect in Go to parse it (use recursive function). The reasons to use reflect and the recursive function are </p>
  
  <ul>
  <li>can have various number of fields (but the first two fields are fixed)</li>
  <li>the field types are not fixed.</li>
  <li>The number of nested layers can be different (in this example only three layers. It can be many more)</li>
  </ul>
  
  <p>Here are some codes.</p>
  
  <pre><code>type Edge struct{
      Uid string `json:"uid,omitempty"`
      Name string `json:"name,omitempty"` 
      Read Article `json:"visited,omitempty"` 
  } 
  type Article struct {
      Uid string`json:"uid,omitempty"` 
      Namestring`json:"name,omitempty"`
      From Site `json:"from,omitempty"`
  }
  type Site struct{
      Uid string `json:"uid,omitempty"`
      Name string `json:"name,omitempty"` 
  }
  func CheckNestedStruct(edges interface{}){ 
      rv := reflect.ValueOf(edges).Elem() 
      uidField := rv.FieldByName("Uid")
      uid := getStructField(edges, "Name") // get value of Name from database 
      if (uid != ""){
          uidField.SetString(uid)
      }
      for i := 0 ; i &lt; rv.NumField() ; i++ {
          field := rv.Field(i)
          fieldType := field.Kind()
          if (fieldType == reflect.Struct){
              CheckNestedStruct(field)
          }
      }
  }
  func main(){
      ....
      var edges Edges{
      ...
      ...
      }
      CheckNestedStruct(&amp;edges)
  }
  </code></pre>
  
  <p>When I ran this, in the first layer I got "<strong>type: *entity.SacWebIS</strong>". However, in the second iteration/recursion, I got "<strong>type: *reflect.rtype</strong>" .
  I also tried <code>field.Interface()</code>. 
  How to modify this code?
  Thanks.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The solution is </p>
  
  <pre><code>CheckNestedStruct(dg, field.Addr().Interface())
  </code></pre>