Memory management Golang中[]字节和字符串转换的技术问题
从Memory management Golang中[]字节和字符串转换的技术问题,memory-management,go,type-conversion,Memory Management,Go,Type Conversion,从字符串转换为[]字节分配新内存是真的吗?另外,从[]字节转换为字符串是否会分配新内存 s := "a very long string" b := []byte(s) // does this doubled the memory requirement? b := []byte{1,2,3,4,5, ...very long bytes..} s := string(b) // does this doubled the memory requirement? 在这两种情况下都是
字符串
转换为[]字节
分配新内存是真的吗?另外,从[]字节
转换为字符串
是否会分配新内存
s := "a very long string"
b := []byte(s) // does this doubled the memory requirement?
b := []byte{1,2,3,4,5, ...very long bytes..}
s := string(b) // does this doubled the memory requirement?
在这两种情况下都是这样
它们是不变的。因此,将它们转换为可变表将分配一个新切片。另见
反过来也一样。否则,对切片进行变异将改变字符串,这将与规范相矛盾。在这两种情况下都是
它们是不变的。因此,将它们转换为可变表将分配一个新切片。另见
反过来也一样。否则,改变切片将改变字符串,这将与规范相矛盾