On Tuesday, January 17, 2017 at 11:07:00 PM UTC-6, Tomi Häsä wrote:
>
> What is the difference between Encoder and MarshalIndent? They both write 
> to a stream. When do you use Encoder?
>

I think julian.klode already answered this, not sure why it has not shown 
up here. 

First, they do *not* both write to a stream. Encoder writes to a stream. 
Marshal and MarshalIndent *return* a byte slice.

They are utility wrapper functions around the Encoder; Marshal sets up an 
encoder, encodes the item to a bytes.Buffer, and returns the byte slice. 
MarshalIndent does exactly the same, but calls the Encoder's Indent 
function with the prefix and indent values first.

So the general answer would be, if you have a stream you are writing to, 
use Encoder. If you need to pass around byte slices or munge them in a way 
that is uncomfortable with streams, use Marshal.

The example for Marshal sends the data to the output stream to get results 
visible when you click Run - it is showing *how* to use Marshal, rather 
than *when* to use Marshal.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to