Skip to main content

Overview

Package bytes implements functions for the manipulation of byte slices. It is analogous to the facilities of the strings package.

Comparison Functions

Equal

Reports whether two byte slices are the same length and contain the same bytes.
func Equal(a, b []byte) bool
a
[]byte
required
First byte slice
b
[]byte
required
Second byte slice
equal
bool
True if slices are equal
Example:
a := []byte("hello")
b := []byte("hello")
if bytes.Equal(a, b) {
    fmt.Println("Equal")
}

Compare

Returns an integer comparing two byte slices lexicographically.
func Compare(a, b []byte) int
result
int
0 if a == b, -1 if a < b, +1 if a > b
Example:
result := bytes.Compare([]byte("a"), []byte("b"))
// result = -1

Search Functions

Contains

Reports whether subslice is within b.
func Contains(b, subslice []byte) bool
Example:
if bytes.Contains([]byte("seafood"), []byte("foo")) {
    fmt.Println("Contains foo")
}

Index

Returns the index of the first instance of sep in s, or -1 if sep is not present.
func Index(s, sep []byte) int
Example:
idx := bytes.Index([]byte("chicken"), []byte("ken"))
// idx = 4

IndexByte

Returns the index of the first instance of c in b, or -1 if c is not present.
func IndexByte(b []byte, c byte) int
Example:
idx := bytes.IndexByte([]byte("hello"), 'e')
// idx = 1

LastIndex

Returns the index of the last instance of sep in s, or -1 if sep is not present.
func LastIndex(s, sep []byte) int

Count

Counts the number of non-overlapping instances of sep in s.
func Count(s, sep []byte) int
Example:
count := bytes.Count([]byte("cheese"), []byte("e"))
// count = 3

Transformation Functions

ToUpper

Returns a copy of the byte slice with all Unicode letters mapped to their upper case.
func ToUpper(s []byte) []byte
Example:
upper := bytes.ToUpper([]byte("hello"))
// upper = []byte("HELLO")

ToLower

Returns a copy of the byte slice with all Unicode letters mapped to their lower case.
func ToLower(s []byte) []byte
Example:
lower := bytes.ToLower([]byte("HELLO"))
// lower = []byte("hello")

Trim

Returns a subslice of s by slicing off all leading and trailing UTF-8-encoded code points contained in cutset.
func Trim(s []byte, cutset string) []byte
Example:
trimmed := bytes.Trim([]byte("!!!Hello!!!"), "!")
// trimmed = []byte("Hello")

TrimSpace

Returns a subslice of s by slicing off all leading and trailing white space.
func TrimSpace(s []byte) []byte
Example:
trimmed := bytes.TrimSpace([]byte("  \t\n Hello \n\t  "))
// trimmed = []byte("Hello")

TrimPrefix

Returns s without the provided leading prefix string.
func TrimPrefix(s, prefix []byte) []byte
Example:
result := bytes.TrimPrefix([]byte("Goodbye!"), []byte("Good"))
// result = []byte("bye!")

TrimSuffix

Returns s without the provided trailing suffix string.
func TrimSuffix(s, suffix []byte) []byte

Replace

Returns a copy of the slice s with the first n non-overlapping instances of old replaced by new.
func Replace(s, old, new []byte, n int) []byte
s
[]byte
required
Source byte slice
old
[]byte
required
Bytes to replace
new
[]byte
required
Replacement bytes
n
int
required
Number of replacements (-1 means all)
Example:
result := bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1)
// result = []byte("moo moo moo")

ReplaceAll

Returns a copy of s with all non-overlapping instances of old replaced by new.
func ReplaceAll(s, old, new []byte) []byte

Split Functions

Split

Slices s into all subslices separated by sep.
func Split(s, sep []byte) [][]byte
Example:
parts := bytes.Split([]byte("a,b,c"), []byte(","))
// parts = [][]byte{[]byte("a"), []byte("b"), []byte("c")}

SplitN

Slices s into subslices separated by sep and returns a slice of the subslices between those separators.
func SplitN(s, sep []byte, n int) [][]byte
n
int
required
Maximum number of subslices to return

SplitAfter

Slices s into all subslices after each instance of sep.
func SplitAfter(s, sep []byte) [][]byte
Example:
parts := bytes.SplitAfter([]byte("a,b,c"), []byte(","))
// parts = [][]byte{[]byte("a,"), []byte("b,"), []byte("c")}

Fields

Splits the slice s around each instance of one or more consecutive white space characters.
func Fields(s []byte) [][]byte
Example:
fields := bytes.Fields([]byte("  foo bar  baz   "))
// fields = [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}

Join Function

Join

Concatenates the elements of s to create a new byte slice.
func Join(s [][]byte, sep []byte) []byte
s
[][]byte
required
Slices to join
sep
[]byte
required
Separator to insert between elements
Example:
parts := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")}
result := bytes.Join(parts, []byte(","))
// result = []byte("foo,bar,baz")

Buffer Type

Buffer

A variable-sized buffer of bytes with Read and Write methods.
type Buffer struct {
    // contains filtered or unexported fields
}

NewBuffer

Creates and initializes a new Buffer using buf as its initial contents.
func NewBuffer(buf []byte) *Buffer
Example:
buf := bytes.NewBuffer([]byte("initial data"))

NewBufferString

Creates and initializes a new Buffer using string s as its initial contents.
func NewBufferString(s string) *Buffer
Example:
buf := bytes.NewBufferString("Hello")

Buffer.Write

Appends the contents of p to the buffer.
func (b *Buffer) Write(p []byte) (n int, err error)
Example:
var buf bytes.Buffer
buf.Write([]byte("Hello, "))
buf.Write([]byte("World!"))
fmt.Println(buf.String()) // Output: Hello, World!

Buffer.WriteString

Appends the contents of s to the buffer.
func (b *Buffer) WriteString(s string) (n int, err error)
Example:
var buf bytes.Buffer
buf.WriteString("Hello, ")
buf.WriteString("World!")

Buffer.WriteByte

Appends the byte c to the buffer.
func (b *Buffer) WriteByte(c byte) error

Buffer.WriteRune

Appends the UTF-8 encoding of Unicode code point r to the buffer.
func (b *Buffer) WriteRune(r rune) (n int, err error)

Buffer.Read

Reads the next len(p) bytes from the buffer or until the buffer is drained.
func (b *Buffer) Read(p []byte) (n int, err error)
Example:
buf := bytes.NewBufferString("Hello")
data := make([]byte, 5)
n, _ := buf.Read(data)
fmt.Printf("Read %d bytes: %s\n", n, data)

Buffer.ReadByte

Reads and returns the next byte from the buffer.
func (b *Buffer) ReadByte() (byte, error)

Buffer.ReadString

Reads until the first occurrence of delim in the input.
func (b *Buffer) ReadString(delim byte) (line string, err error)

Buffer.String

Returns the contents of the unread portion of the buffer as a string.
func (b *Buffer) String() string
Example:
var buf bytes.Buffer
buf.WriteString("Hello")
fmt.Println(buf.String()) // Output: Hello

Buffer.Bytes

Returns a slice of length b.Len() holding the unread portion of the buffer.
func (b *Buffer) Bytes() []byte

Buffer.Len

Returns the number of bytes of the unread portion of the buffer.
func (b *Buffer) Len() int

Buffer.Reset

Resets the buffer to be empty.
func (b *Buffer) Reset()
Example:
var buf bytes.Buffer
buf.WriteString("Hello")
buf.Reset()
fmt.Println(buf.Len()) // Output: 0

Buffer.Grow

Grows the buffer’s capacity to guarantee space for another n bytes.
func (b *Buffer) Grow(n int)

Complete Example

package main

import (
    "bytes"
    "fmt"
)

func main() {
    // Comparison
    a := []byte("hello")
    b := []byte("hello")
    fmt.Println(bytes.Equal(a, b)) // true
    
    // Searching
    data := []byte("Hello, World!")
    if bytes.Contains(data, []byte("World")) {
        fmt.Println("Found World")
    }
    
    // Transformation
    upper := bytes.ToUpper([]byte("hello"))
    fmt.Printf("Uppercase: %s\n", upper)
    
    // Replacement
    result := bytes.ReplaceAll([]byte("foo foo foo"), []byte("foo"), []byte("bar"))
    fmt.Printf("Replaced: %s\n", result)
    
    // Splitting
    parts := bytes.Split([]byte("a,b,c"), []byte(","))
    for _, part := range parts {
        fmt.Printf("Part: %s\n", part)
    }
    
    // Buffer usage
    var buf bytes.Buffer
    buf.WriteString("Hello, ")
    buf.WriteString("World!")
    fmt.Println(buf.String())
    
    // Reading from buffer
    buf = *bytes.NewBufferString("test data")
    line, _ := buf.ReadString(' ')
    fmt.Printf("Read: %s\n", line)
}

Build docs developers (and LLMs) love