For example,

package main

import "fmt"

func main() {
    myString := "Hello there"
    slice := make([]byte, 1, 1+len(myString))
    slice[0] = byte(len(myString))
    slice = append(slice, myString...)
    fmt.Println(slice[0], string(slice[1:]))
}

Output:

11 Hello there
Answer from peterSO on Stack Overflow
🌐
Medium
medium.com › @tyler_brewer2 › bits-bytes-and-byte-slices-in-go-8a99012dcc8f
Bits, Bytes, and Byte Slices in Go | by Tyler Brewer | Medium
February 7, 2022 - UTF-8 Character Set (note: the decimals are listed in their hexidecimal value. You can print the hexidecimal value of a byte slice by using % x when printing) Go · Golang · Programming · Web Development · Software Development · 96 followers · ·133 following ·
🌐
Go
go.dev › blog › slices-intro
Go Slices: usage and internals - The Go Programming Language
Slicing is done by specifying a half-open range with two indices separated by a colon. For example, the expression b[1:4] creates a slice including elements 1 through 3 of b (the indices of the resulting slice will be 0 through 2). b := []byte{'g', 'o', 'l', 'a', 'n', 'g'} // b[1:4] == []byte{'o', ...
🌐
GeeksforGeeks
geeksforgeeks.org › go language › how-to-split-a-slice-of-bytes-in-golang
How to split a slice of bytes in Golang? - GeeksforGeeks
November 12, 2024 - In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function.
🌐
YourBasic
yourbasic.org › golang › convert-string-to-byte-slice
Convert between byte array/slice and string · YourBasic Go
yourbasic.org/golang · Basics · Convert string to bytes · Convert bytes to string · Performance · When you convert between a string and a byte slice (array), you get a brand new slice that contains the same bytes as the string, and vice versa.
🌐
Go Packages
pkg.go.dev › bytes
bytes package - bytes - Go Packages
Package bytes implements functions for the manipulation of byte slices.
🌐
Reddit
reddit.com › r/golang › reading and writing slice of byte slices ([][]byte)
r/golang on Reddit: Reading and writing slice of byte slices ([][]byte)
March 25, 2022 -

We have an app that reads from a zmq PULL socket using goczmq. As we get data its of the format [][]byte.

The number of slices in the array is variable and also the size of each of the slice is variable.

I have a requirement to write it in a file and then read it back on. So I though of a file format where I write each slice as:

1-byte-slice length

4-byte-element-length

[variable size data]

4-byte-element-length

[variable size data] .... (end of first dataframe)

1-byte-slice length

4-byte-element-length

[variable size data]

4-byte-element-length

[variable size data]

I wrote a simple code that seems to do the job: https://go.dev/play/p/C1KU_G5DwKr

But I trying to figure out if there is a better way or am I reinventing the wheel and I could just use some library.

Top answer
1 of 3
3
Have you considered https://pkg.go.dev/encoding/gob#Encoder.Encode ?
2 of 3
2
There's a lot to consider here and only you can know if your solution is good enough. Can you be sure that you're never read/writing to the file in parallel? I'd suggest protecting operations on the file with a mutex. Is there a reason for you to write the file and then read it back? Does some other process do something with the file in the meantime? Couldn't you just hold the data in ram instead of reading it back immediately after you wrote it? This would increase your applications performance drastically if it's a e.g. long running daemon. Is the data really unstructured? Is []byte the best representation for the data or could you give it more structure? What do you need the 1-byte-slice-length for? You could also NUL terminate the datagrams so you could leave the slice-length of. If optimizing write performance is one of your goals writing the data in chunks across datagram boundaries would be an easy way to archive this because it would eliminate a lot of write syscalls. If your handle a lot of io operations the gc could get under a lot of pressure because of all the byte slices so instead you could use a sync.Pool to handle your buffers. You're also not reading the data in an efficient way. Inside the readDataFrame function you know the size to be numElements long, yet you just append to this slice possibly causing lots of resizing. Instead just dataFrame := make([][]byte, numElements) and inside the loop write to it dataFrame[i] = data. Hope this helps.
🌐
GitHub
github.com › go101 › go101 › wiki › Some-details-about-byte-slices-and-rune-slices-in-Go
Some details about byte slices and rune slices in Go
October 24, 2022 - And it looks official Go documentations also call a byte slice as a slice of bytes and a rune slice as a slice of runes. Both of the terminologies byte slice and slice of bytes exist in both Go specification and the builtin standard package docs. From contextes, it looks these documentations view the two terminologies as the same thing.
Author: go101
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-trim-a-slice-of-bytes-in-golang
How to trim a slice of bytes in Golang? | GeeksforGeeks
February 23, 2023 - In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function.
🌐
LabEx
labex.io › tutorials › go-how-to-handle-go-byte-slice-conversion-446113
How to handle Go byte slice conversion | LabEx
In the world of Golang, byte slice conversion is a critical skill for developers working with low-level data processing and memory management.
🌐
Dot Net Perls
dotnetperls.com › bytes-go
Go - bytes: Slices and Methods - Dot Net Perls
We use methods, like append(), to build byte slices. We can specify them with string literals.
🌐
GeeksforGeeks
geeksforgeeks.org › go language › how-to-compare-two-slices-of-bytes-in-golang
How to compare two slices of bytes in Golang? - GeeksforGeeks
July 12, 2025 - To directly check if two byte slices are equal, use bytes.Equal, which returns true if the slices are identical.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-join-the-elements-of-the-byte-slice-in-golang
How to join the elements of the byte slice in Golang? | GeeksforGeeks
August 26, 2019 - The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to join the elements of the byte slice with ...
🌐
ZetCode
zetcode.com › golang › byte
Working with Bytes in Go
May 3, 2025 - To facilitate byte-level manipulations, Go offers the bytes package, which implements a collection of functions for efficient handling of byte slices. The bytes package shares similarities with the strings package but operates on byte slices rather than string values.
🌐
TutorialEdge.net
tutorialedge.net › home › golang development › converting byte slices to strings in go
Converting Byte Slices to Strings in Go | TutorialEdge.net
June 10, 2020 - In order to achieve the conversion we can use the built-in string function which takes in a byte slice and returns a string value!
🌐
gosamples
gosamples.dev › tutorials › convert string to []byte or []byte to string in go
🖨️ Convert string to []byte or []byte to string in Go
October 13, 2021 - To convert a string to a byte slice in Go, use the standard []byte conversion expression []byte(string).