Documentation
¶
Overview ¶
Package coding contains structures for encoding and decoding values.
Index ¶
- Variables
- type Decoder
- func (d *Decoder) DecodeBool() (bool, error)
- func (d *Decoder) DecodeData() ([]byte, error)
- func (d *Decoder) DecodeFloat32() (float32, error)
- func (d *Decoder) DecodeFloat64() (float64, error)
- func (d *Decoder) DecodeInt() (int, error)
- func (d *Decoder) DecodeInt16() (int16, error)
- func (d *Decoder) DecodeInt32() (int32, error)
- func (d *Decoder) DecodeInt64() (int64, error)
- func (d *Decoder) DecodeInt8() (int8, error)
- func (d *Decoder) DecodeString() (string, error)
- func (d *Decoder) DecodeUint() (uint, error)
- func (d *Decoder) DecodeUint16() (uint16, error)
- func (d *Decoder) DecodeUint32() (uint32, error)
- func (d *Decoder) DecodeUint64() (uint64, error)
- func (d *Decoder) DecodeUint8() (uint8, error)
- func (d *Decoder) Decompress() error
- func (d Decoder) Validate() error
- type Encoder
- func (e *Encoder) Compress() ([]byte, error)
- func (e Encoder) Data() []byte
- func (e *Encoder) EncodeBool(b bool)
- func (e *Encoder) EncodeData(b []byte)
- func (e *Encoder) EncodeFloat32(f float32)
- func (e *Encoder) EncodeFloat64(f float64)
- func (e *Encoder) EncodeInt(n int)
- func (e *Encoder) EncodeInt16(n int16)
- func (e *Encoder) EncodeInt32(n int32)
- func (e *Encoder) EncodeInt64(n int64)
- func (e *Encoder) EncodeInt8(n int8)
- func (e *Encoder) EncodeString(s string)
- func (e *Encoder) EncodeUint(n uint)
- func (e *Encoder) EncodeUint16(n uint16)
- func (e *Encoder) EncodeUint32(n uint32)
- func (e *Encoder) EncodeUint64(n uint64)
- func (e *Encoder) EncodeUint8(n uint8)
- func (e *Encoder) Flush()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEOB is an end of buffer error. ErrEOB error = errors.New("end of buffer") // ErrType is an incorrect type error. ErrType error = errors.New("incorrect type") // ErrByteLength is an incorrect byte length error. ErrByteLength error = errors.New("incorrect byte length") // ErrCRC is a CRC check error. ErrCRC = errors.New("crc check failed") )
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder types decode bytes and keep track of an offset.
func NewDecoder ¶
NewDecoder creates and returns a new decoder with the given data.
func (*Decoder) DecodeBool ¶
DecodeBool decodes the next value as a boolean.
func (*Decoder) DecodeData ¶
DecodeData decodes the next value as a byte array.
func (*Decoder) DecodeFloat32 ¶
DecodeFloat32 decodes the next value as a floating point number.
func (*Decoder) DecodeFloat64 ¶
DecodeFloat64 decodes the next value as a floating point number.
func (*Decoder) DecodeInt16 ¶
DecodeInt16 decodes the next value as an integer.
func (*Decoder) DecodeInt32 ¶
DecodeInt32 decodes the next value as an integer.
func (*Decoder) DecodeInt64 ¶
DecodeInt64 decodes the next value as an integer.
func (*Decoder) DecodeInt8 ¶
DecodeInt8 decodes the next value as an integer.
func (*Decoder) DecodeString ¶
DecodeString decodes the next value as a string.
func (*Decoder) DecodeUint ¶
DecodeUint decodes the next value as an integer.
func (*Decoder) DecodeUint16 ¶
DecodeUint16 decodes the next value as an integer.
func (*Decoder) DecodeUint32 ¶
DecodeUint32 decodes the next value as an integer.
func (*Decoder) DecodeUint64 ¶
DecodeUint64 decodes the next value as an integer.
func (*Decoder) DecodeUint8 ¶
DecodeUint8 decodes the next value as an integer.
func (*Decoder) Decompress ¶
Decompress decompresses the decoder's data and places the result in data.
Example ¶
e := NewEncoder() e.EncodeString("{ \"name\": \"pi\" }") e.EncodeFloat64(math.Pi) e.EncodeString("{ \"name\": \"phi\" }") e.EncodeFloat64(math.Phi) e.EncodeString("{ \"name\": \"e\" }") e.EncodeFloat64(math.E) e.EncodeString("{ \"name\": \"ln(2)\" }") e.EncodeFloat64(math.Ln2) fmt.Printf("Bytes: %d\n", len(e.Data())) cd, err := e.Compress() if err != nil { fmt.Printf("Error compressing data: %s\n", err) return } fmt.Printf("Compressed bytes: %d\n", len(cd)) fmt.Printf("Change: %0.f%%\n", 100.0*float64(len(cd)-len(e.data))/float64(len(e.data))) d := NewDecoder(cd) if err = d.Decompress(); err != nil { fmt.Printf("Error decompressing data: %s\n", err) return } if err = d.Validate(); err != nil { fmt.Printf("Error validating data: %s\n", err) return } var s string if s, err = d.DecodeString(); err != nil { fmt.Printf("Error decoding string: %s\n", err) return } fmt.Printf("String: %s\n", s) var f float64 if f, err = d.DecodeFloat64(); err != nil { fmt.Printf("Error decoding float: %s\n", err) return } fmt.Printf("Float: %f\n", f)
Output: Bytes: 153 Compressed bytes: 106 Change: -28% String: { "name": "pi" } Float: 3.141593
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder types encode encode values to binary data.
func (*Encoder) Compress ¶
Compress compresses the encoder's data and returns the result.
Compress calls the encoder's Data function so that its data's CRC is included in the compressed bytes.
func (*Encoder) EncodeFloat32 ¶
EncodeFloat32 encodes a float.
func (*Encoder) EncodeFloat64 ¶
EncodeFloat64 encodes a float.
func (*Encoder) EncodeInt16 ¶
EncodeInt16 encodes an integer.
func (*Encoder) EncodeInt32 ¶
EncodeInt32 encodes an integer.
func (*Encoder) EncodeInt64 ¶
EncodeInt64 encodes an integer.
func (*Encoder) EncodeString ¶
EncodeString encodes the string.
func (*Encoder) EncodeUint16 ¶
EncodeUint16 encodes an integer.
func (*Encoder) EncodeUint32 ¶
EncodeUint32 encodes an integer.
func (*Encoder) EncodeUint64 ¶
EncodeUint64 encodes an integer.
func (*Encoder) EncodeUint8 ¶
EncodeUint8 encodes an integer.