bytes

package
v2.303.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package bytes provides byte-oriented helpers used across go-service.

Most identifiers in this package are thin wrappers or aliases around the standard library `bytes` package. They exist to:

  • provide a consistent import path within go-service, and
  • centralize a small set of byte/string conversion utilities.

Aliases and wrappers

`Buffer` is an alias for `bytes.Buffer`, and constructors like `NewBuffer`, `NewBufferString`, and `NewReader` delegate directly to the standard library.

Zero-copy conversions

This package also exposes `String`, which converts a `[]byte` to a `string` without allocating. This is an advanced, performance-oriented helper with important safety constraints; see `String` for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone added in v2.240.0

func Clone(b []byte) []byte

Clone returns a copy of b.

This is a thin wrapper around bytes.Clone. Unlike helpers that may return subslices, Clone always allocates a new slice (unless b is nil, in which case it returns nil).

func NewBuffer

func NewBuffer(buf []byte) *bytes.Buffer

NewBuffer returns a new Buffer initialized with buf's contents.

This is a thin wrapper around bytes.NewBuffer. The returned buffer uses buf as its initial contents; subsequent writes may grow the buffer.

func NewBufferString

func NewBufferString(s string) *bytes.Buffer

NewBufferString returns a new Buffer initialized with the contents of s.

This is a thin wrapper around bytes.NewBufferString.

func NewReader

func NewReader(b []byte) *bytes.Reader

NewReader returns a new bytes.Reader reading from b.

This is a thin wrapper around bytes.NewReader. The returned reader reads from b without copying.

func String

func String(b []byte) string

String converts b to a string without copying.

Safety and lifetime

The returned string aliases the memory backing b. That means:

  • You MUST NOT modify the contents of b after calling String(b).
  • The returned string is only valid while b remains alive (reachable) and its backing array is not reused.

Violating these constraints can lead to surprising behavior, data races, or memory safety issues. Use this helper only when you control the lifecycle of b and need to avoid an allocation. If you need an owning copy, use `string(b)` instead.

func TrimSpace

func TrimSpace(s []byte) []byte

TrimSpace returns a subslice of s with all leading and trailing white space removed.

This is a thin wrapper around bytes.TrimSpace. The returned slice may refer to the same underlying array as s.

Types

type Buffer

type Buffer = bytes.Buffer

Buffer is an alias for bytes.Buffer.

It is provided so go-service code can depend on a consistent import path while still using the standard library implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL