title: Unmarshal json to reflected struct
body: <p>Is it possible to unmarshal JSON into a struct made from reflection without hardcoding the original type?</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
    "fmt"
    "encoding/json"
    "reflect"
  )
  
  type Employee struct {
    Firstname string     `json:"firstname"`
  }
  
  func main() {
    //Original struct
    orig := new(Employee)
  
    t := reflect.TypeOf(orig)
    v := reflect.New(t.Elem())
  
    //Reflected struct
    new := v.Elem().Interface().(Employee)
  
    // Unmarshal to reflected struct
    json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), &amp;new)
  
    fmt.Printf("%+v\n", new)
  }
  </code></pre>
  
  <p>I used a cast to <code>Employee</code> in this example. But what if i don't know the type? </p>
  
  <p>When i just use <code>v</code> for the unmarhaling the struct will be zeroed. </p>
  
  <pre><code>json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), v)
  </code></pre>
  
  <p>When I omit the cast I get a map. which is understandable</p>
  
  <pre><code>json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), v.Elem().Interface())
  </code></pre>