title: How do I JSON serialize a struct with ambiguous embedding in Go?
body: <p>It would seem that having both <code>Identity</code> and <code>Report</code> embed <code>CommonProperties</code> causes omission of all of the common fields in the JSON serialization of the <code>CompositeObject</code>.</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "time"
  )
  
  type CommonProperties struct {
      Type         string    `json:"type,omitempty"`
      ID           string    `json:"id,omitempty"`
      CreatedByRef string    `json:"created_by_ref,omitempty"`
      Created      time.Time `json:"created,omitempty"`
      Modified     time.Time `json:"modified,omitempty"`
      Revoked      bool      `json:"revoked,omitempty"`
      Labels       []string  `json:"labels,omitempty"`
      Confidence   int       `json:"confidence,omitempty"`
      Language     string    `json:"lang,omitempty"`
  }
  
  type Identity struct {
      CommonProperties
      Name               string   `json:"name,omitempty"`
      Description        string   `json:"description,omitempty"`
      IdentityClass      string   `json:"identity_class,omitempty"`
      Sectors            []string `json:"sectors,omitempty"`
      ContactInformation string   `json:"contact_information,omitempty"`
  }
  
  type Report struct {
      CommonProperties
      Name        string    `json:"name,omitempty"`
      Description string    `json:"description,omitempty"`
      Published   time.Time `json:"published,omitempty"`
      ObjectRefs  []string  `json:"object_refs,omitempty"`
  }
  
  type CompositeObject struct {
      InternalID string `json:"uid,omitempty"`
      Pending    bool   `json:"pending"`
      Type       string `json:"type"`
  
      *Identity
      *Report
  }
  
  func main() {
      co := CompositeObject{
          Type:    "report",
          Pending: true,
          Report: &amp;Report{
              Name:        "Spear Phishing Report",
              Description: "...",
              Published:   time.Date(2016, 5, 12, 8, 17, 27, 0, time.UTC),
              ObjectRefs: []string{
                  "indicator--26ffb872-1dd9-446e-b6f5-d58527e5b5d2",
                  "campaign--83422c77-904c-4dc1-aff5-5c38f3a2c55c",
                  "relationship--f82356ae-fe6c-437c-9c24-6b64314ae68a",
              },
              CommonProperties: CommonProperties{
                  ID:           "report--84e4d88f-44ea-4bcd-bbf3-b2c1c320bcb3",
                  Type:         "report",
                  Created:      time.Date(2016, 5, 12, 8, 17, 27, 0, time.UTC),
                  CreatedByRef: "identity--a463ffb3-1bd9-4d94-b02d-74e4f1658283",
                  Modified:     time.Date(2016, 5, 12, 8, 17, 27, 0, time.UTC),
              },
          },
      }
      json, _ := json.Marshal(&amp;co)
      fmt.Println(string(json))
  }
  </code></pre>
  
  <p>Play links:</p>
  
  <ul>
  <li><a href="https://play.golang.org/p/iVxtYHWVPw" rel="nofollow noreferrer">https://play.golang.org/p/iVxtYHWVPw</a></li>
  <li><a href="https://play.golang.org/p/6adrrJ9iac" rel="nofollow noreferrer">https://play.golang.org/p/6adrrJ9iac</a></li>
  </ul>
  
  <p>The second one gets the desired output by commenting out second <code>CommonProperties</code> embed.</p>
  
  <p>I don't understand why an embed w/ this type of ambiguity would prevent JSON serialization. Is there a solution that doesn't involve writing a bunch of <code>MarshalJSON()</code> functions?</p>