Avatar ·

How to convert a null-terminated byte array to a string in Go?

Hello everyone! I just started learning Go and encountered a problem when working with C-strings. I need to read data from an external source (for example, from syscall or a C library via cgo) that returns a byte array terminated by a null byte (`\0`). In Go, strings are just slices of bytes, but they don't have to be null-terminated, and a null byte inside a string is considered a regular character. I have this code: ```go package main import "fmt" func main() { // Example: byte array obtained from a C function data := []byte{'H', 'e', 'l', 'l', 'o', 0, 'W', 'o', 'r', 'l', 'd', 0} // Simple conversion to string str := string(data) fmt.Println(str) // Will it print "Hello" and garbage? or "Hello\0World"? } ``` The problem is that if I view