GoLang: Quick gist on Difference between Marshal, Unmarshal, Encoder and Decoder

Summary: Marshal, Unmarshal and Encode, Decode

Summary: Marshal, Unmarshal and Encode, Decode

  • Marshal: Convert struct/map interface data into JSON.
  • Unmarshal: Convert JSON data into struct/map

If reading from a file involved, then you read it in []byte format. To read from a file

  • using os.Open open the file.

  • then read all the data using io.ReadAll method.

  • Encoder and Decoder kind of do reading/writing internally. More like a simplified version of Marshal and Unmarshal.

  • Encoder converts the struct/map into JSON and automatically writes into io.Writer

  • Decoder reads from io.Writer and convert the JSON data into struct/map interface.

If there is a byte slice to work with, use Marshal and Unmarshal. If there is an io.Writer or an io.Reader, use Encode and Decode.

Ref: https://blog.devgenius.io/to-unmarshal-or-to-decode-json-processing-in-go-explained-e92fab5b648f

Comments

Popular posts from this blog

grep: unknown device method

Uploading files to FTP/SFTP using CURL

How to find outgoing IP in Linux ?