Documentation
¶
Overview ¶
Package binary implements utility functions to encode and decode values to and from a byte slice.
Index ¶
- Variables
- func AppendBool(b []byte, on bool) []byte
- func AppendBytes(b, bytes []byte) []byte
- func AppendDuration(b []byte, d time.Duration) []byte
- func AppendFloat32(b []byte, f float32) []byte
- func AppendFloat64(b []byte, f float64) []byte
- func AppendInt(b []byte, i int) []byte
- func AppendInt16(b []byte, i int16) []byte
- func AppendInt32(b []byte, i int32) []byte
- func AppendInt64(b []byte, i int64) []byte
- func AppendPtr(b []byte, o Marshaler) []byte
- func AppendString(b []byte, str string) []byte
- func AppendTime(b []byte, t time.Time) []byte
- func AppendUint16(b []byte, i uint16) []byte
- func AppendUint32(b []byte, i uint32) []byte
- func AppendUint64(b []byte, i uint64) []byte
- func Bits(bits ...bool) int64
- func GetBytes(b []byte) (data, remaining []byte)
- func GetNBytes(b []byte, n int) (data, remaining []byte)
- func ScanBool(b []byte) ([]byte, bool)
- func ScanBytes(b []byte) ([]byte, []byte)
- func ScanDuration(b []byte) ([]byte, time.Duration)
- func ScanFloat32(b []byte) ([]byte, float32)
- func ScanFloat64(b []byte) ([]byte, float64)
- func ScanInt(b []byte) ([]byte, int)
- func ScanInt16(b []byte) ([]byte, int16)
- func ScanInt32(b []byte) ([]byte, int32)
- func ScanInt64(b []byte) ([]byte, int64)
- func ScanPtr(b []byte, ctor func() Unmarshaler) []byte
- func ScanString(b []byte) ([]byte, string)
- func ScanTime(b []byte) ([]byte, time.Time)
- func ScanUint16(b []byte) ([]byte, uint16)
- func ScanUint32(b []byte) ([]byte, uint32)
- func ScanUint64(b []byte) ([]byte, uint64)
- type Marshaler
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
var BigEndian = binary.BigEndian
var LittleEndian = binary.LittleEndian
var TooShort = errors.New("byte slice is too short")
TooShort should be returned by Unmarshalers when a Scan function returns a nil byte slice.
Functions ¶
func AppendBool ¶
AppendBool encodes bool on to buffer b as a single byte where 0 is false and 1 is true.
func AppendBytes ¶
AppendBytes encodes the bytes slice to buffer b by first writing the length of the bytes slice followed by the byte slice iteself.
func AppendDuration ¶
AppendDuration encodes time.Duration d by casting it to an int64 and calling AppendInt64.
func AppendFloat32 ¶
AppendFloat32 encodes the float f to buffer b.
func AppendFloat64 ¶
AppendFloat64 encodes the float f to buffer b.
func AppendInt16 ¶
AppendInt16 encodes int16 i to buffer b.
func AppendInt32 ¶
AppendInt32 encodes int32 i to buffer b.
func AppendInt64 ¶
AppendInt64 encodes int64 i to buffer b.
func AppendString ¶
AppendString encodes string str to buffer b by writing the length of str as an int64 followed by the string's bytes.
func AppendTime ¶
AppendTime encodes time.Time t to buffer b by calling t's MarshalBinary method and appending the resulting bytes.
func AppendUint16 ¶
AppendUint16 encodes uint16 i to buffer b.
func AppendUint32 ¶
AppendUint32 encodes uint32 i to buffer b.
func AppendUint64 ¶
AppendUint64 encodes uint64 i to buffer b.
func Bits ¶
Bits returns an int64 whose bits are set to 1 when the corresponding bits parameter is true. E.g. Bite(true, true, false, true) results in b1011. Bits will panic when more than 63 bool values are povided as parameters.
func GetBytes ¶
GetBytes decodes a byte slice from buffer b and returns the slice followed by the remaining bytes. The encoded value is assumed to be a length followed by that many bytes. If there is insufficient data, both return values will be nil.
func GetNBytes ¶
GetNBytes decodes n bytes from buffer b and returns the slice followed by the remaining bytes. If there is insufficient data, both return values will be nil.
func ScanDuration ¶
ScanDuration decodes a time.Duration encoded using AppendDuration.
func ScanFloat32 ¶
ScanFloat32 decodes a float from buffer b encoded using AppendFloat32.
func ScanFloat64 ¶
ScanFloat32 decodes a float from buffer b encoded using AppendFloat64.
func ScanPtr ¶
func ScanPtr(b []byte, ctor func() Unmarshaler) []byte
ScanPtr decodes an object encoded using AppendPtr. It first reads a bool indicating whether the encoded value is nil. When the value is not nil, it calls ctor method to receive an Unmarshaler. Then is calls Unmarshal to decode the object.
func ScanString ¶
ScanString decodes a string encoded using AppendString.
func ScanUint16 ¶
ScanUint16 decodes a uint16 encoded using AppendUint16.
func ScanUint32 ¶
ScanUint32 decodes a uint32 encoded using AppendUint32.
func ScanUint64 ¶
ScanUint64 decodes a uint32 encoded using AppendUint64.
Types ¶
type Marshaler ¶
type Marshaler interface {
// MarshalAppend encodes itself and append the resulting bytes to
// the provided byte slice and return the new byte slice.
MarshalAppend([]byte) []byte
}
Marshaler defines an interface with a MarshalAppend method.
type Unmarshaler ¶
type Unmarshaler interface {
// Unmarshal decodes itself from the provided byte slice returning any
// bytes remaining. TooShort is returned when the provided slice is
// too short. Other errors are returned when decoding fails.
Unmarshal([]byte) ([]byte, error)
}
Unmarshaler defines an interface with an Unmarshal method. TooShort should be returned when a Scan function returns a nil byte slice.