To convert a string to a byte slice in Go, you can use the []byte()
type conversion. Here's an example:
package main
import (
"fmt"
)
func main() {
str := "Hello, world!"
bytes := []byte(str)
fmt.Printf("%v\n", bytes) // [72 101 108 108 111 44 32 119 111 114 108 100 33]
}
In this example, the string "Hello, world!" is converted to a byte slice using []byte(str)
. The resulting byte slice [72 101 108 108 111 44 32 119 111 114 108 100 33]
represents the ASCII values of each character in the string.