Documentation
¶
Overview ¶
Package buffers provides tools for working with byte array, and byte slice buffers, for the Go programming language.
Example ¶
Here is an example of buffers.Writer being used to provide a io.Writer interface to an byte array, and byte slice:
import "github.com/reiver/go-buffers"
// ...
var buffer [1024]byte
var p []byte = buffer[:]
writer := buffers.NewWriter(p)
n, err := writer.Write(data)
switch casted := err.(type) {
case buffers.TooShort:
//@TODO
default:
return err
}
Index ¶
- Variables
- type Buffer
- func (receiver *Buffer) Len() int
- func (receiver *Buffer) ReadFrom(r io.Reader) (int64, error)
- func (receiver *Buffer) String() string
- func (receiver *Buffer) Write(p []byte) (n int, err error)
- func (receiver *Buffer) WriteByte(c byte) error
- func (receiver *Buffer) WriteRune(r rune) (int, error)
- func (receiver *Buffer) WriteString(s string) (int, error)
- type TooShort
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
BufferOverflow = fck.Error("buffer overflow")
)
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
A Buffer is used to store a series of bytes.
A Buffer can be used as is, without any type of initialization (if not doesn't want the Buffer to have a limit):
var buffer buffers.Buffer
But if one wants the Buffer to have a limit, then it should be initialized like the following:
var buffer buffers.Buffer = buffers.LimitedBuffer(4194304) // 4 MB
func LimitedBuffer ¶
LimitedBuffer returns a Buffer with a maximum size.
var buffer buffers.Buffer = buffers.LimitedBuffer(4194304) // 4 MB
func (*Buffer) Len ¶
Len returns how many bytes have been accumulated in Buffer.
receiver.Len() == len(receiver.String())
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer provides an io.Writer interface to a byte array, and byte slice.
This is something that should probably exist in the Go built-in "bytes" library, but doesn't.
Example ¶
package main
import (
"github.com/reiver/go-buffers"
"fmt"
)
func main() {
var buffer [256]byte
var p []byte = buffer[:]
writer := buffers.NewWriter(p)
data := []byte("Hello world!")
n, err := writer.Write(data)
if nil != err {
fmt.Printf("ERROR: Problem writing: %s\n", err)
return
}
fmt.Printf("Wrote %d bytes to the buffer.\n", n)
fmt.Printf("Those %d bytes in the buffer have a value of “%s”.\n", n, buffer)
Output: