Documentation
¶
Overview ¶
Package binp is a fast performant bytes packing and unpacking module, to read and write primitive Go types from and to []byte, without using the unsafe package.
Gilles Van Vlasselaer https://mrwaggel.be
Index ¶
- Constants
- Variables
- func Pack(values ...interface{}) ([]byte, error)
- func PackFixed(size int, values ...interface{}) (b []byte, err error)
- func PackNetwork(values ...interface{}) ([]byte, error)
- func PackNetworkFixed(size int, values ...interface{}) (b []byte, err error)
- func PackTo(b []byte, values ...interface{}) error
- func Read(b []byte, receivers ...interface{}) error
- func Size(values []interface{}) (int, error)
Constants ¶
const ( Int = byte(iota) + 1 IntSlice Int64 Int64Slice Int32 Int32Slice Int16 Int16Slice Int8 Int8Slice Uint UintSlice Uint64 Uint64Slice Uint32 Uint32Slice Uint16 Uint16Slice Byte ByteSlice Float64 Float64Slice Float32 Float32Slice BoolTrue BoolFalse BoolSlice String StringSlice )
These byte constants are used to identify the next sequence of bytes according to the type it represents.
Variables ¶
var ( ErrUnsupportedType = errors.New("unsupported type") ErrExpectedTypeIdentifier = errors.New("expected type identifier") )
Errors returned by package functions can be tested against these errors.
Functions ¶
func PackFixed ¶
PackFixed packs all the given values into a slice of bytes, this is useful if it is known that all the primitive data types are of fixed length, e.g. no strings, or []bytes. Has a slight performance boost as it skips counting the size of all values.
Note: For every primitive data type that has to packed, the size has to be incremented with 1, that extra byte is used as the datatype identifier, except for boolean values. See Size().
Example: Packing an int64 and a bool would take up 10bytes.
PackFixed(10, int64(123), true)
1 byte that holds the identifier for the int64, 8 bytes that holds the int64, 1 byte that holds the bool.
func PackNetwork ¶
PackNetwork packs all the given values into a slice of bytes, whereas the first 4 bytes contain the length of all the values.
func PackNetworkFixed ¶
PackNetworkFixed packs all the given values into a slice of bytes.
See PackFixed and PackNetwork
func PackTo ¶
PackTo packs all the given values into the given b ([]byte).
Warning: make sure you allocated enough capacity to the []byte or runtime panics will occur. See the functions Size() and PackFixed() to calculate the needed capacity.
Types ¶
This section is empty.