Skip to content

Instantly share code, notes, and snippets.

@rogpeppe
Created January 24, 2017 14:02
Show Gist options
  • Save rogpeppe/e965528b2e4942a65e800dd02167a4de to your computer and use it in GitHub Desktop.
Save rogpeppe/e965528b2e4942a65e800dd02167a4de to your computer and use it in GitHub Desktop.

Codecs in Go

At this dojo, we'll concentrate on exploring techniques for mapping encoded values to and from Go values.

We'll focus on JSON because the principles are similar across several different codec packages and JSON is well known. Codec packages in Go usually provide rules for mapping a Go types to and from an encoding.

Exercises:

  1. Encode a Go string to JSON.

    • import encoding/json
    • import os
    • json.Marshal
    • os.Stdout.Write(data)
  2. Try it with a different types

    • an integer?
    • a float?
    • a slice?
    • a function?
    • a map?
    • Always check your errors!
  3. Decode a JSON string into a Go string.

    • use back-quoted string literal to put JSON in code.
    • json.Unmarshal
    • remember to unmarshal into a pointer to the object!
  4. Encode a Go struct into JSON

    • type T struct { ... }
    • what's unusual about the JSON produced?
    • what happens when you try to fix it?
  5. Rename fields with struct tags

  6. Decode a JSON object into a Go struct

  7. Encode a Go struct as JSON but omit empty strings when encoded.

    • use omitempty flag.
  8. Decode the JSON values held in the file json1.json, json2.json and json3.json.

    • Try decoding into the empty interface (interface{})
    • Print some of the values in the data structure.
    • Experiment with http://json2struct.mervine.net/
    • When might that not be appropriate?
  9. Decode the JSON value held in the file json4.json

  10. Given the following struct type, add methods to make it encode and decode as a dot-separated version number. For example Version{1, 2, 4} should encode as "1.2.4".

     type Version struct {
     	Major, Minor, Patch int
     }
    
  11. Given the following struct type, define methods on it to decode the JSON in json5.json to it.

    type Shapes []Shape
    type Shape interface {
    	Draw()
    }
    
  12. Experiment with other codecs.

    • See gopkg.in/yaml.v2
    • See gopg.in/mgo.v2/bson
    • encoding/xml
    • encoding/binary
    • github.com/rogpeppe/rjson
    • How are they the same?
    • How are they different?
  13. (advanced!) Define a codec for this type: https://godoc.org/github.com/mvdan/sh/syntax#Stmt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment