title: in golang, general function to load http form data into a struct
body: <p>In Go, http form data (e.g. from a POST or PUT request) can be accessed as a map of the form <code>map[string][]string</code>.  I'm having a hard time converting this to structs in a generalizable way.</p>
  
  <p>For example, I want to load a map like:</p>
  
  <pre><code>m := map[string][]string {
      "Age": []string{"20"},
      "Name": []string{"John Smith"},
  }
  </code></pre>
  
  <p>Into a model like:</p>
  
  <pre><code>type Person struct {
      Age   int
      Name string
  }
  </code></pre>
  
  <p>So I'm trying to write a function with the signature <code>LoadModel(obj interface{}, m map[string][]string) []error</code> that will load the form data into an interface{} that I can type cast back to a Person.  Using reflection so that I can use it on any struct type with any fields, not just a Person, and so that I can convert the string from the http data to an int, boolean, etc as necessary.</p>
  
  <p>Using the answer to this question <a href="https://stackoverflow.com/questions/6395076/in-golang-using-reflect-how-do-you-set-the-value-of-a-struct-field">in golang, using reflect, how do you set the value of a struct field?</a> I can set the value of a person using reflect, e.g.:</p>
  
  <pre><code>p := Person{25, "John"}
  reflect.ValueOf(&amp;p).Elem().Field(1).SetString("Dave")
  </code></pre>
  
  <p>But then I'd have to copy the load function for every type of struct I have.  When I try it for an interface{} it doesn't work.</p>
  
  <pre><code>pi := (interface{})(p)
  reflect.ValueOf(&amp;pi).Elem().Field(1).SetString("Dave")
  // panic: reflect: call of reflect.Value.Field on interface Value
  </code></pre>
  
  <p>How can I do this in the general case? Or even better, is there a more idiomatic Go way to accomplish what I'm trying to do?</p>