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.
-
Encode a Go string to JSON.
- import encoding/json
- import os
- json.Marshal
- os.Stdout.Write(data)
-
Try it with a different types
- an integer?
- a float?
- a slice?
- a function?
- a map?
- Always check your errors!
-
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!
-
Encode a Go struct into JSON
- type T struct { ... }
- what's unusual about the JSON produced?
- what happens when you try to fix it?
-
Rename fields with struct tags
- A "struct tag" is a string literal following a struct field
- See https://golang.org/pkg/reflect/#StructTag
- Use backquotes
- go vet will tell you about malformed values.
-
Decode a JSON object into a Go struct
-
Encode a Go struct as JSON but omit empty strings when encoded.
- use omitempty flag.
-
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?
-
Decode the JSON value held in the file json4.json
- See https://golang.org/pkg/encoding/json/#Decoder.UseNumber
- https://golang.org/pkg/math/big/#Int
- big.Int does not know about JSON? How does it manage to do it.
- https://golang.org/pkg/encoding/#TextUnmarshaler
-
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 }
-
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() }
- You'll need to decode in two stages.
- See https://golang.org/pkg/encoding/json/#Marshaler
- See https://golang.org/pkg/encoding/json/#Unmarshaler
- See https://golang.org/pkg/encoding/json/#RawMessage
-
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?
-
(advanced!) Define a codec for this type: https://godoc.org/github.com/mvdan/sh/syntax#Stmt
- problem described by Daniel https://groups.google.com/d/msg/golang-nuts/iAJEj7kIbtc/DxNmSBkOFQAJ
- See https://golang.org/pkg/reflect/#StructOf
- or fork encoding/json
- or... ?