utilgo.mau.fi/util/exbytes Index | Examples | Files

package exbytes

import "go.mau.fi/util/exbytes"

Index

Examples

Variables

var ErrWriterBufferFull = errors.New("exbytes.Writer: buffer full")

Types

type Writer

type Writer []byte

Writer is a simple byte writer that does not allow extending the buffer.

Writes always go after the current len() and will fail if the slice capacity is too low.

The correct way to use this is to create a slice with sufficient capacity and zero length:

x := make([]byte, 0, 11)
w := (*Writer)(&x)
w.Write([]byte("hello"))
w.WriteByte(' ')
w.WriteString("world")
fmt.Println(string(x)) // "hello world"
Example

Code:play 

package main

import (
	"fmt"

	"go.mau.fi/util/exbytes"
)

func main() {
	x := make([]byte, 0, 11)
	w := (*exbytes.Writer)(&x)
	w.Write([]byte("hello"))
	w.WriteByte(' ')
	w.WriteString("world")
	fmt.Println(string(x))
}

Output:

hello world

func (*Writer) String

func (w *Writer) String() string

func (*Writer) Write

func (w *Writer) Write(b []byte) (n int, err error)

func (*Writer) WriteByte

func (w *Writer) WriteByte(r byte) error

func (*Writer) WriteString

func (w *Writer) WriteString(s string) (n int, err error)

Source Files

writer.go

Version
v0.8.6 (latest)
Published
Mar 16, 2025
Platform
linux/amd64
Imports
3 packages
Last checked
1 week ago

Tools for package owners.