go-encoding is a Go package which abstracts various encoding mechanisms under a unified API.
$ go get github.com/Shopify/go-encodingEncodings in this package follow either or both of these interfaces:
typeValueEncodinginterface {
Encode(datainterface{}) ([]byte, error)
Decode(b []byte, datainterface{}) error
}
typeStreamEncodinginterface {
StreamEncode(datainterface{}, w io.Writer) errorStreamDecode(r io.Reader, datainterface{}) error
}
typeByteArrayEncodinginterface {
Encode([]byte) ([]byte, error)
Decode([]byte) ([]byte, error)
}
typeByteStreamEncodinginterface {
StreamEncode(io.Writer) (io.WriteCloser, error)
StreamDecode(io.Reader) (io.ReadCloser, error)
}See example_test.go for how it all fits together.
Used to encode arbitrary Go types to a byte array.
| Name | ValueEncoding | StreamEncoding | Implementation |
|---|---|---|---|
| Literal | ✔ | strconv | |
| Json | ✔ | ✔ | encoding/json |
| Gob | ✔ | encoding/gob |
To use a ValueEncoding as a StreamEncoding, you can wrap with NewStreamEncoding.
Or vice-versa by using NewValueEncoding.
Used to encode byte arrays to byte arrays, which is useful to do processing like compression, encryption, or base64.
| Name | ByteArrayEncoding | ByteStreamEncoding | Implementation |
|---|---|---|---|
| Base32 | ✔ | ✔ | encoding/base32 |
| Base64 | ✔ | ✔ | encoding/base64 |
| Flate | ✔ | ✔ | compress/flate |
| Gzip | ✔ | ✔ | compress/gzip |
| Hex | ✔ | ✔ | encoding/hex |
| Noop | ✔ | ✔ | passthrough |
| Zlib | ✔ | ✔ | compress/zlib |