~~~
func byte2string2(in [16]byte) []byte {
tmp := make([]byte, 16)
for i, value := range in {
tmp[i] = value
}
return tmp
}
~~~
还有更快的方法吗???
package main
func main() {
in := [16]int{1, 1, 1}
tmp := make([]int, 16)
copy(tmp, in[:])
tmp[0] = 2
print(in[0]) //1
print(tmp[0]) //2
}
如果你要复制数组,而不是直接引用的话
#4
更多评论
我知道了
参考:https://segmentfault.com/a/1190000005006351
~~~
func byte2string2(in [16]byte) []byte {
tmp := make([]byte, 0)
x := (*[3]uintptr)(unsafe.Pointer(&tmp))
x[0] = uintptr(unsafe.Pointer(&in))
x[1] = 16
x[2] = 16
return tmp
}
~~~
#1