title: identify the type of an object dynamically while reading json data
body: <p>Quite new to go, sorry if this question sounds obvious.</p>
  
  <p>I would like to use reflection in order to identify the type of an object while reading a json file. </p>
  
  <p>The use case (please see the code below) is the following: I have two structs BoyGift and GirlGift that contain different fields. I have also a Boolean indicator IsBoy that is true if the recipient of the giftis a boy, false otherwise. </p>
  
  <p>The type that encapsulates this behavior is the type Gift:</p>
  
  <pre><code>//Gift type
  type Gift struct {
      IsBoy   bool     `json:"isBoy"`
      Payload ??? `json:"payload"`
  }
  </code></pre>
  
  <p>that holds the data. How can I define that type in order the json unmarshal to convert dynamically to the correct type? The "json schema" in this case defines that a Gift should be either a BoyGift or a GirlGift. Is it possible to do this via reflection? How? </p>
  
  <p>Doing unmarshal twice would be great, if the Boolean info is known</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
  )
  
  //BoyGift type
  type BoyGift struct {
      Cars  uint32 `json:"cars"`
      Balls uint32 `json:"balls"`
  }
  
  //GirlGift type
  type GirlGift struct {
      Dolls uint32 `json:"dolls"`
      Lego  uint32 `json:"lego"`
  }
  
  //Gift type
  type Gift struct {
      IsBoy   bool     `json:"isBoy"`
      Payload GirlGift `json:"payload"`
  }
  
  func main() {
  
      b := []byte(`{
          "isBoy": true,
          "payload": {
            "cars": 1,
            "balls": 2
          }
        }`)
  
      var g Gift
      err := json.Unmarshal(b, &amp;g)
  
      if err != nil {
          fmt.Println(err)
          return
      }
  
      fmt.Println(g)
  }
  </code></pre>