Standard library
Bytes
Byte sequences: length, slice, concat, and conversions.
Introduction
Byte sequences: length, slice, concat, and conversions. Import std/bytes to bind the module as bytes. Call free functions as bytes.name(...). Struct methods use a receiver value.
/ std/bytes
This page is the package reference for std/bytes. Private helpers used only by co-located tests are not listed.
Functions
Free functions on bytes. Each function has a short description, an example, then parameters and return shape.
len
Returns the length of a bytes value. Call form: bytes.len(b).
/ std/bytes
$ n = bytes.len(b'hi')
Parameters: b: bytes value. Returns: Integer length.
is_empty
Reports whether a bytes value is empty. Call form: bytes.is_empty(b).
/ std/bytes
$ empty = bytes.is_empty(b'')
Parameters: b: bytes value. Returns: Boolean.
get
Result-shaped: ok is the byte at index i as ui8; error is "out of bounds". Call form: bytes.get(b, i).
/ std/bytes
| bytes.get(b'AB', 1) {
$ x { }
! e { }
}Parameters: b: bytes. i: zero-based index. Returns: Result. Ok arm: byte as ui8. Err arm: "out of bounds".
slice
Result-shaped half-open byte range [start, end). Error is "out of bounds". Call form: bytes.slice(b, start, end).
/ std/bytes
| bytes.slice(b'hello', 1, 4) {
$ part { }
! e { }
}Parameters: b: bytes. start: inclusive index. end: exclusive index. Returns: Result. Ok arm: sliced bytes. Err arm: "out of bounds".
cat
Concatenates two byte sequences. Call form: bytes.cat(a, b).
/ std/bytes
$ c = bytes.cat(b'a', b'b')
Parameters: a: left bytes. b: right bytes. Returns: Concatenated bytes.
from_int
Packs an integer into bytes. Call form: bytes.from_int(n).
/ std/bytes
$ b = bytes.from_int(42)
Parameters: n: integer to pack. Returns: Bytes representation.
from_str
Encodes a string as UTF-8 bytes. Call form: bytes.from_str(s).
/ std/bytes
$ b = bytes.from_str("hi")Parameters: s: string to encode. Returns: UTF-8 bytes.