title: What is the best way to handle "a single element or an array" JSON property in Go?
body: <p>Now we have a JSON HTTP request data which would be a single element like
  <code>{"data": {"id":1}}</code> OR an array of elements like <code>{"data": [{"id":1}, {"id":2}]}</code>.</p>
  
  <p>Since a client side cannot change the implementation, we have to keep and accept this data structure.</p>
  
  <p>Currently I implement struct like:</p>
  
  <pre><code>type Request struct {
    rawData json.RawMessage `json:"data"`
  
    Data *Data `json:"-"`
    DataList []*Data `json:"-"`
  }
  </code></pre>
  
  <p>And first parse "data" property as json.RawMessage into variable <code>req</code>, try parsing as a single element first, then if it fails try parsing as an array.</p>
  
  <pre><code>if err := json.Unmarshal(req.rawData, &amp;req.Data); err != nil {
      if err := json.Unmarshal(req.RawData, &amp;req.DataList); err != nil {
          return errors.New("could not parse data")
      } else if len(req.DataList) &lt; 1 {
          return errors.New("empty list")
      }
  }
  </code></pre>
  
  <p>In this case, later when we want to use this data, we can check if <code>rawData</code> is a single element or an array by <code>len(req.Datalist) &gt;= 1</code>. (Or I can set some flag to show that like <code>isDataMultiple</code> in the struct, when parsing).</p>
  
  <p>Is there any idiomatic way to achieve the same result in Go other than what I'm doing?</p>