Convert String to Byte Array in Golang
-
Using the
byte()
Function to Convert String to Byte Array in Golang -
Using
[]byte(strToConvert)
to Convert String to Byte Array in Golang -
Using the
copy()
Function to Convert String to Byte Array in Golang -
Using
[]byte
to Convert String to Byte Slice in Golang

You’re not alone if you’re new to Go and wonder why this is a byte array. When I initially started learning to Go, it was one of the first things a computer scientist acquaintance asked me.
You obtain a slice containing the string’s bytes when you convert a string to a byte array in Golang. A string in Go is just a read-only byte slice. A string can carry any number of bytes; therefore, it’s important to say that right away.
Unicode text, UTF-8 text, or any other specified format is unnecessary. A string is identical to a slice of bytes in terms of its content.
Here are some helpful hints for working with strings in Go.
Using the byte()
Function to Convert String to Byte Array in Golang
Use the byte()
function in Golang to convert a String to a Byte array. A byte is an unsigned 8-bit integer. An array is returned by the byte()
method, which takes a string as an input.
When you make a string, you’re actually making an array of bytes. As a result, you can read individual bytes as if they were an array.
The following code, for example, iterates through every byte in a string and outputs it as both a string and a byte.
package main
import "fmt"
func main() {
str := "hello boss!"
data := []byte(str)
fmt.Println(data)
}
Output:
[104 101 108 108 111 32 98 111 115 115 33]
Using []byte(strToConvert)
to Convert String to Byte Array in Golang
In this example, we will be using []byte(strToConvert)
to convert string to byte array in Golang.
package main
import (
"fmt"
)
func main() {
var strToConvert string
strToConvert = "hello boss!"
byteString := []byte(strToConvert)
fmt.Println(byteString)
}
Output:
[104 101 108 108 111 32 98 111 115 115 33]
Using the copy()
Function to Convert String to Byte Array in Golang
In this example, the string will be copied to a byte array using the copy()
function. As a result, we declared a byte array and copied the string to it using the copy function.
package main
import (
"fmt"
)
func main() {
var strToConvert string
strToConvert = "hello boss!"
byteString := make([]byte, len(strToConvert))
copy(byteString, strToConvert)
fmt.Println(byteString)
}
Output:
[104 101 108 108 111 32 98 111 115 115 33]
Using []byte
to Convert String to Byte Slice in Golang
A string can be converted to a byte slice []byte
in Go, and a byte slice can be converted back to a string. It is a simple process that appears to be identical to any other type of change.
This conversion is frequently used to feed a string into a function that takes byte slices or a byte slice into a function that expects a string.
package main
import "fmt"
func main() {
var s string = "hello boss!"
fmt.Println(s)
var b []byte
b = []byte(s)
fmt.Println(b)
for i := range b {
fmt.Println(string(b[i]))
}
s = string(b)
fmt.Println(s)
}
Output:
hello boss!
[104 101 108 108 111 32 98 111 115 115 33]
h
e
l
l
o
b
o
s
s
!
hello boss!
Related Article - Go String
- Convert String to Integer Type in Go
- Convert an Int Value to String in Go
- Parse Date String in Go
- Efficiently Concatenate Strings in Go
- Write Multiline Strings in Go
- Read a File Into String in Go