我需要将字符串数组转换为字节数组数组。这段代码有效,但重复append
对我来说似乎令人反感。有没有更好的办法?
input := []string{"foo", "bar"}
output := [][]byte{}
for _, str := range input {
output = append(output, []byte(str))
}
fmt.Println(output) // [[102 111 111] [98 97 114]]
我需要将字符串数组转换为字节数组数组。这段代码有效,但重复append
对我来说似乎令人反感。有没有更好的办法?
input := []string{"foo", "bar"}
output := [][]byte{}
for _, str := range input {
output = append(output, []byte(str))
}
fmt.Println(output) // [[102 111 111] [98 97 114]]