title: Implement io.WriterTo with JSON
body: <p>I found this cool interface recently, <code>io.WriterTo</code>:</p>
  <p><a href="https://godocs.io/io#WriterTo" rel="nofollow noreferrer">https://godocs.io/io#WriterTo</a></p>
  <p>I would like to implement it for some JSON objects. I was able to make this:</p>
  <pre><code>package calendar
  
  import (
     &quot;bytes&quot;
     &quot;encoding/json&quot;
     &quot;io&quot;
  )
  
  type date struct {
     Month int
     Day int
  }
  
  func (d date) WriteTo(w io.Writer) (int64, error) {
     buf := new(bytes.Buffer)
     err := json.NewEncoder(buf).Encode(d)
     if err != nil {
        return 0, err
     }
     return buf.WriteTo(w)
  }
  </code></pre>
  <p>But I think it's not ideal, as it makes a copy of the object in memory, before sending to the Writer. Is it possible to write directly, but also know how many bytes were written?</p>