title: Can I use MarshalJSON to add arbitrary fields to a json encoding in golang?
body: <p>Suppose I've written the following code snippet. Full code on the playground <a href="http://play.golang.org/p/6uDf6nbrx_" rel="noreferrer">here</a> for those inclined.</p>
  
  <pre><code>type Book struct {
    Title        string
    Author       string
  }
  
  func main() {
    ms := Book{"Catch-22", "Joseph Heller"}
    out, err := json.MarshalIndent(ms, "", "  ")
    if err != nil {
      log.Fatalln(err)
    }
    fmt.Println(string(out))
  }
  </code></pre>
  
  <p>This code outputs the following, exactly as I'd expect:</p>
  
  <pre><code>{
    "Title": "Catch-22",
    "Author": "Joseph Heller"
  }
  </code></pre>
  
  <p>Suppose for a moment I wanted to add a field to the JSON output without including it in the <code>Book</code> struct. Perhaps a genre:</p>
  
  <pre><code>{
    "Title": "Catch-22",
    "Author": "Joseph Heller",
    "Genre": "Satire"
  }
  </code></pre>
  
  <p>Can I use <code>MarshalJSON()</code> to add an arbitrary field to the JSON payload on <code>Marshal()</code>? Something like:</p>
  
  <pre><code>func (b *Book) MarshalJSON() ([]byte, error) {
      // some code
  }
  </code></pre>
  
  <p>Other <a href="https://stackoverflow.com/a/11129633/877115">answers</a> make me think this should be possible, but I'm struggling to figure out the implementation. </p>