title: How to unmarshal struct into map in golang
body: <p>I have a Json: </p>
  
  <pre><code>{
    "id": "me",
    "name": "myname",
    "planets": {
        "EARTH": 3,
        "MARS": 4
    }
  }
  </code></pre>
  
  <p>I don't know how to unmarshal planets field into map[string]int, so I'll get access to the elements without unmarshaling them too like in this example: <a href="https://stackoverflow.com/questions/11066946/partly-json-unmarshal-into-a-map-in-go">json to map Golang</a></p>
  
  <p>Here is the code: </p>
  
  <pre><code>package main
  
  import (
     "encoding/json"
     "fmt"
    )
  
  type User struct {
     ID      string `json:"id"`
     Name    string `json:"name"`
     Planets struct {
         Planet map[string]*json.RawMessage
      } `json:"planets"`
  }
  
   func main() {
      data := `{
        "id": "me",
        "name": "myname",
        "planets": {
        "EARTH": 3,
         "MARS": 4
        }
      }`
  
     user := &amp;User{}
     err := json.Unmarshal([]byte(data), user)
     if err != nil {
         fmt.Println("ERROR " + err.Error())
     }
      fmt.Println(user.ID)
      fmt.Println(user.Planets.Planet["EARTH"])
    }
  </code></pre>
  
  <p>fmt.Println(user.Planets.Planet["EARTH"]) - returns </p>
  
  <p>fmt.Println(user.Planets["EARTH"]) - does not support indexing</p>