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:
type ValueEncoding interface {
Encode(data interface{}) ([]byte, error)
Decode(b []byte, data interface{}) error
}
type StreamEncoding interface {
StreamEncode(data interface{}, w io.Writer) error
StreamDecode(r io.Reader, data interface{}) error
}
type ByteArrayEncoding interface {
Encode([]byte) ([]byte, error)
Decode([]byte) ([]byte, error)
}
type ByteStreamEncoding interface {
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 |