<p dir="auto">When encoding a large data structure (such as a tree representing a file-system), the Encode() methods first marshal the encoded data into a []byte buffer and then write the buffer to the writer.  Similarly, the Decode() methods require a buffer to unmarshal from. These buffers must be allocated (and grown) and typically more than double the memory consumption of the process. This is limiting for applications which are sized to the machine/container.</p>
<p dir="auto">I propose that the interfaces are extended to support streaming encoding/decoding of data. For each type (except slices and maps), the data would be separately marshaled and written. For slices and maps, each entry would be marshaled and written, rather the slice/map as a whole.</p>
<p dir="auto">Some API suggestions/alternatives:<br>
func Encode(w io.Writer, v interface{}) error<br>
func NewStreamingEncoder(w io.Writer) *Encoder<br>
func (enc *Encoder) StreamingEncode(v interface{}) error</p>
<p dir="auto">Gc correctly rejects the following program:</p>
<div class="highlight highlight-source-go notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="package n
func n(){_=string(0.)}"><pre class="notranslate"><span class="pl-k">package</span> n
<span class="pl-k">func</span> <span class="pl-en">n</span>(){<span class="pl-s1">_</span><span class="pl-c1">=</span><span class="pl-en">string</span>(<span class="pl-c1">0.</span>)}</pre></div>
<p dir="auto">But gotype successfully compiles it. It must not. <code class="notranslate">0.</code> is an fp constant.<br>
on commit <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/golang/go/commit/e9a746de6e61b9b823a981843c0d98bc209eed2b/hovercard" href="https://github.com/golang/go/commit/e9a746de6e61b9b823a981843c0d98bc209eed2b"><tt>e9a746d</tt></a></p>