Documentation ¶
Index ¶
- Variables
- func B2S(b []byte) string
- func Copy(b []byte) []byte
- func S2B(s string) []byte
- type Buffer
- func (buf *Buffer) Cap() int
- func (buf *Buffer) Full() []byte
- func (buf *Buffer) Grow(sz int)
- func (buf *Buffer) Guarantee(sz int)
- func (buf *Buffer) Len() int
- func (buf *Buffer) ReadFrom(r io.Reader) (int64, error)
- func (buf *Buffer) Reset()
- func (buf *Buffer) String() string
- func (buf *Buffer) Truncate(n int)
- func (buf *Buffer) Write(b []byte) (int, error)
- func (buf *Buffer) WriteAt(b []byte, start int64) (int, error)
- func (buf *Buffer) WriteByte(c byte) error
- func (buf *Buffer) WriteRune(r rune) (int, error)
- func (buf *Buffer) WriteString(s string) (int, error)
- func (buf *Buffer) WriteStringAt(s string, start int64) (int, error)
- func (buf *Buffer) WriteTo(w io.Writer) (int64, error)
- type ReadNopCloser
- type Reader
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBeyondBufferLen is returned if .WriteAt() is attempted beyond buffer length. ErrBeyondBufferLen = errors.New("start beyond buffer length") )
Functions ¶
func B2S ¶
B2S returns a string representation of []byte without allocation.
According to the Go spec strings are immutable and byte slices are not. The way this gets implemented is strings under the hood are:
type StringHeader struct { Data uintptr Len int }
while slices are:
type SliceHeader struct { Data uintptr Len int Cap int }
because being mutable, you can change the data, length etc, but the string has to promise to be read-only to all who get copies of it.
So in practice when you do a conversion of `string(byteSlice)` it actually performs an allocation because it has to copy the contents of the byte slice into a safe read-only state.
Being that the shared fields are in the same struct indices (no different offsets), means that if you have a byte slice you can "forcibly" cast it to a string. Which in a lot of situations can be risky, because then it means you have a string that is NOT immutable, as if someone changes the data in the originating byte slice then the string will reflect that change! Now while this does seem hacky, and it _kind_ of is, it is something that you see performed in the standard library. If you look at the definition for `strings.Builder{}.String()` you'll see this :)
Types ¶
type Buffer ¶
type Buffer struct{ B []byte }
Buffer is a simple wrapper around a byte slice.
func (*Buffer) Full ¶ added in v1.0.2
Full returns the full capacity byteslice allocated for this buffer.
func (*Buffer) Grow ¶
Grow will increase the buffers length by 'sz', and the capacity by at least this.
func (*Buffer) Guarantee ¶
Guarantee will guarantee buffer containers at least 'sz' remaining capacity.
func (*Buffer) ReadFrom ¶ added in v1.2.0
ReadFrom will read bytes from reader into buffer, fulfilling io.ReaderFrom.
func (*Buffer) Reset ¶
func (buf *Buffer) Reset()
Reset will reset the buffer length to 0 (retains capacity).
func (*Buffer) String ¶
String returns the underlying byte slice as a string. Please note this value is tied directly to the underlying byte slice, if you write to the buffer then returned string values will also change.
To get an immutable string from buffered data, use string(buf.B).
func (*Buffer) WriteAt ¶
WriteAt will append given byte slice to buffer at index 'start', fulfilling io.WriterAt.
func (*Buffer) WriteString ¶
WriteString will append given string to buffer, fulfilling io.StringWriter.
func (*Buffer) WriteStringAt ¶
WriteStringAt will append given string to buffer at index 'start'.
type ReadNopCloser ¶ added in v1.1.2
type ReadNopCloser struct {
Reader
}
ReadNopCloser wraps a Reader{} to provide nop close method.
func (*ReadNopCloser) Close ¶ added in v1.1.2
func (*ReadNopCloser) Close() error