title: Using reflection to populate a pointer to a struct
body: <p>I'd like to iterate over the fields in a struct and prompt for string values to string fields, doing this recursively for fields that are pointers to structs. </p>
  
  <p>Currently this is what I've tried, but I get an error when trying to set this value in the pointer's string field.</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "fmt"
      "reflect"
  )
  
  type Table struct {
      PK *Field
  }
  
  type Field struct {
      Name string
  }
  
  
  func main() {
      PopulateStruct(&amp;Table{})
  }
  
  func PopulateStruct(a interface{}) interface {} {
      typeOf := reflect.TypeOf(a)
      valueOf := reflect.ValueOf(a)
      for i := 0; i &lt; typeOf.Elem().NumField(); i++ {
          switch typeOf.Elem().Field(i).Type.Kind() {
          case reflect.String:
              fmt.Print(typeOf.Elem().Field(i).Name)
              var s string
              fmt.Scanf("%s", &amp;s)
              valueOf.Elem().Field(i).SetString(s)
          case reflect.Ptr:
              ptr := reflect.New(valueOf.Elem().Field(i).Type())
              PopulateStruct(ptr.Elem().Interface())
              valueOf.Elem().Field(i).Set(ptr)
          }
      }
  }
  </code></pre>
  
  <p>Expecting the return value to include an initialised struct with the pointers string field set. </p>
  
  <p>Getting an error when setting the pointer's string field.</p>
  
  <p>panic: reflect: call of reflect.Value.Field on zero Value</p>