Documentation ¶
Overview ¶
Package container gives you a []byte slice on steroids, allowing for quick data appending, prepending and fetching as well as transparent error transportation.
A Container is basically a [][]byte slice that just appends new []byte slices and only copies things around when necessary.
Byte slices added to the Container are not changed or appended, to not corrupt any other data that may be before and after the given slice. If interested, consider the following example to understand why this is important:
package main import ( "fmt" ) func main() { a := []byte{0, 1,2,3,4,5,6,7,8,9} fmt.Printf("a: %+v\n", a) fmt.Printf("\nmaking changes...\n(we are not changing a directly)\n\n") b := a[2:6] c := append(b, 10, 11) fmt.Printf("b: %+v\n", b) fmt.Printf("c: %+v\n", c) fmt.Printf("a: %+v\n", a) }
run it here: https://play.golang.org/p/xu1BXT3QYeE
Index ¶
- type Container
- func (c *Container) Append(data []byte)
- func (c *Container) AppendAsBlock(data []byte)
- func (c *Container) AppendContainer(data *Container)
- func (c *Container) AppendContainerAsBlock(data *Container)
- func (c *Container) AppendInt(n int)
- func (c *Container) AppendNumber(n uint64)
- func (c *Container) CompileData() []byte
- func (c *Container) Get(n int) ([]byte, error)
- func (c *Container) GetAll() []byte
- func (c *Container) GetAsContainer(n int) (*Container, error)
- func (c *Container) GetMax(n int) []byte
- func (c *Container) GetNextBlock() ([]byte, error)
- func (c *Container) GetNextBlockAsContainer() (*Container, error)
- func (c *Container) GetNextN16() (uint16, error)
- func (c *Container) GetNextN32() (uint32, error)
- func (c *Container) GetNextN64() (uint64, error)
- func (c *Container) GetNextN8() (uint8, error)
- func (c *Container) HoldsData() bool
- func (c *Container) Length() (length int)
- func (c *Container) MarshalJSON() ([]byte, error)
- func (c *Container) Peek(n int) []byte
- func (c *Container) PeekContainer(n int) (newC *Container)
- func (c *Container) Prepend(data []byte)
- func (c *Container) PrependAsBlock(data []byte)
- func (c *Container) PrependInt(n int)
- func (c *Container) PrependLength()
- func (c *Container) PrependNumber(n uint64)
- func (c *Container) Replace(data []byte)
- func (c *Container) UnmarshalJSON(data []byte) error
- func (c *Container) WriteAllTo(writer io.Writer) error
- func (c *Container) WriteToSlice(slice []byte) (n int, containerEmptied bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is []byte sclie on steroids, allowing for quick data appending, prepending and fetching.
func New ¶
New creates a new container with an optional initial []byte slice. Data will NOT be copied.
func NewContainer ¶
NewContainer is DEPRECATED, please use New(), it's the same thing.
func (*Container) AppendAsBlock ¶
AppendAsBlock appends the length of the data and the data itself. Data will NOT be copied.
func (*Container) AppendContainer ¶ added in v0.4.1
AppendContainer appends another Container. Data will NOT be copied.
func (*Container) AppendContainerAsBlock ¶ added in v0.4.1
AppendContainerAsBlock appends another Container (length and data). Data will NOT be copied.
func (*Container) AppendNumber ¶
AppendNumber appends a number (varint encoded).
func (*Container) CompileData ¶
CompileData concatenates all bytes held by the container and returns it as one single []byte slice. Data will NOT be copied and is NOT consumed.
func (*Container) GetAll ¶ added in v0.12.0
GetAll returns all data. Data MAY be copied and IS consumed.
func (*Container) GetAsContainer ¶ added in v0.4.1
GetAsContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS consumed.
func (*Container) GetMax ¶
GetMax returns as much as possible, but the given amount of bytes at maximum. Data MAY be copied and IS consumed.
func (*Container) GetNextBlock ¶
GetNextBlock returns the next block of data defined by a varint. Data MAY be copied and IS consumed.
func (*Container) GetNextBlockAsContainer ¶ added in v0.4.1
GetNextBlockAsContainer returns the next block of data as a Container defined by a varint. Data will NOT be copied and IS consumed.
func (*Container) GetNextN16 ¶
GetNextN16 parses and returns a varint of type uint16.
func (*Container) GetNextN32 ¶
GetNextN32 parses and returns a varint of type uint32.
func (*Container) GetNextN64 ¶
GetNextN64 parses and returns a varint of type uint64.
func (*Container) HoldsData ¶ added in v0.12.0
HoldsData returns true if the Container holds any data.
func (*Container) MarshalJSON ¶ added in v0.4.1
MarshalJSON serializes the container as a JSON byte array.
func (*Container) Peek ¶ added in v0.15.0
Peek returns the given amount of bytes. Data MAY be copied and IS NOT consumed.
func (*Container) PeekContainer ¶ added in v0.15.0
PeekContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS NOT consumed.
func (*Container) PrependAsBlock ¶ added in v0.12.0
PrependAsBlock prepends the length of the data and the data itself. Data will NOT be copied.
func (*Container) PrependInt ¶ added in v0.12.0
PrependInt prepends an int (varint encoded).
func (*Container) PrependLength ¶
func (c *Container) PrependLength()
PrependLength prepends the current full length of all bytes in the container.
func (*Container) PrependNumber ¶ added in v0.12.0
PrependNumber prepends a number (varint encoded).
func (*Container) Replace ¶
Replace replaces all held data with a new data slice. Data will NOT be copied.
func (*Container) UnmarshalJSON ¶ added in v0.4.1
UnmarshalJSON unserializes a container from a JSON byte array.
func (*Container) WriteAllTo ¶ added in v0.4.1
WriteAllTo writes all the data to the given io.Writer. Data IS NOT copied (but may be by writer) and IS NOT consumed.