There are two ways to convert a string to a byte array in Golang.
- Using []byte(): It returns a new byte slice containing the same bytes.
- Using []rune(): It converts a string into a range of Unicode code points representing runes.
Go byte is an unsigned 8-bit integer with an uint8 data type.
Golang string is a sequence of variable characters where each character is represented by one or more bytes using UTF-8 Encoding.
In Golang, you get a slice containing the string’s bytes to convert a string to a byte array.
In Go, a string is, in effect, a read-only slice of bytes. Therefore, it’s essential to state that a string holds arbitrary bytes.
A byte is an 8-bit unsigned int.
It is not required to hold Unicode text, UTF-8 text, or any other predefined format. As far as the content of a string is concerned, it is precisely equivalent to a slice of bytes. In Golang, we often use byte slices.
Method 1: Using []byte()
To convert string to byte array in Golang, use the byte() function. The byte() function takes a string as input and returns the array.
package main import "fmt" func main() { str := "MBB$" data := []byte(str) fmt.Println(data) }
Output
go run hello.go [77 66 66 36]
In the code, first, we defined the string MBB$ and then converted that string into a slice of bytes. Then, use the fmt.Println() method to print the array of bytes.
Golang bytes.Index()
The bytes.Index() is a function in the Go programming language (Golang) package “bytes”. It searches for the first instance of sub in s and returns the index of the first occurrence in s of sub or -1 if the sub is not present in “s”.
Here we import the “bytes” package at the top (in the import statement). We call bytes.Index() to locate the sequence with the byte values.
// hello.go package main import ( "bytes" "fmt" ) func main() { values := []byte("Pug") // Search for this byte sequence. result := bytes.Index(values, []byte("g")) fmt.Println(result) // This byte sequence is not found. result = bytes.Index(values, []byte("Dog")) if result == -1 { fmt.Println("Dog not found") } }
Output
go run hello.go 2 Dog not found
Golang strings.Index() method, bytes.Index() function returns the index if a sequence matches. Otherwise, it returns -1.
In the first example, we find g matches Pug, so it returns the index of g in Pug, which is two because the index starts with 0.
In the second example, we don’t find the Dog sequence in Pug; that is why it returns Dog not found.
Golang copy string to a byte slice
Golang copy() is a built-in method to copy the string into a byte slice.
// hello.go package main import "fmt" func main() { // Create an empty byte slice of length 4. values := make([]byte, 4) // Copy string into bytes. animal := "pug" copied := copy(values, animal) fmt.Println(copied) fmt.Println(values) }
Output
go run hello.go 3 [112 117 103 0]
Here we create an empty 4-element byte slice. Then we copy a three-element string into it.
Method 2: Using []rune()
To convert a string into a range of Unicode code points representing runes, you can use the []rune() method.
package main
import (
"fmt"
)
func main() {
str := "kb"
runes := []rune(str)
fmt.Println(runes)
}
Output
[107 98]
The above code will generate a slice of runes containing the same characters as the string, regardless of the number of bytes required to represent each character.
A new duplicate of the data will be created when a string is converted to a byte slice or a slice of runes. Therefore, changing the slice will not impact the original string.
That’s it for this tutorial.