To convert a byte slice to a UTF-8 string in Go using the bytes package, you can use the ToString()
function from the bytes
package. Here's an example:
package main
import (
"bytes"
"fmt"
)
func main() {
byteSlice := []byte{104, 101, 108, 108, 111} // A byte slice, representing the string "hello"
str := bytes.ToString(byteSlice)
fmt.Println(str) // Output: hello
}
In this example, we have a byte slice byteSlice
that represents the string "hello" in UTF-8 encoding. We use the ToString()
function from the bytes
package to convert the byte slice to a UTF-8 string. The resulting string is then printed, resulting in the output "hello".