How to convert a string to a slice of bytes ([]byte) in Go?
Hi everyone! I just started learning Go and ran into a problem. I need to write a string into a byte array, or more precisely — into a `[]byte` slice. I know that in Go strings are an immutable sequence of bytes in UTF-8 encoding, and a byte slice is a mutable structure. But I don't understand how to connect them.
Here's what I tried:
```go
package main
import "fmt"
func main() {
var s string = "Hello, Go!"
var b []byte
// First attempt: just assign
b = s // Error: cannot use s (type string) as type []byte in assignment
// Second attempt: create a slice and copy
b = make([]byte, len(s))
copy(b, s) // This worked, but I'm not sure if it's correct
fmt.Println(b)
}
```
Question: how to correctly and efficiently convert a string to a byte slice?