在Golang中将字节0附加到字节数组末尾的最佳方法是什么?
我已经尝试过这些解决方案:
data := []byte(location)
empty := []byte{0}
locationBytes = append(data, empty[0])
//locationBytes = append(data, ""...) // not working
我的情况是将字符串传输到字节数组。但是字节数组的结尾没有
\0
作为终止符号。
最佳答案
您可以将0直接添加到字节数组中,而无需创建空的:= [] byte {0}
data := []byte(location)
locationBytes = append(data, 0)
关于go - 在Golang中将字节0附加到字节数组末尾的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61519659/