数组扩容golang
package main
import (
"fmt"
"unsafe"
)
func main() {
which := make([]byte, 0)
which = append(which, []byte("123")...)
which1 := which
fmt.Printf("which: %s var pointer :%p arr pointer %p cap: %d len:%d, which1: %s var pointer :%p arr pointer %p cap: %d len:%d\n",
which, &which, unsafe.Pointer(&which[0]), cap(which), len(which), which1, &which1, unsafe.Pointer(&which1[0]), cap(which1), len(which1))
which = append(which, []byte("456")...)
fmt.Printf("which: %s var pointer :%p arr pointer %p cap: %d len:%d, which1: %s var pointer :%p arr pointer %p cap: %d len:%d\n",
which, &which, unsafe.Pointer(&which[0]), cap(which), len(which), which1, &which1, unsafe.Pointer(&which1[0]), cap(which1), len(which1))
which1 = append(which1, []byte("789")...)
fmt.Printf("which: %s var pointer :%p arr pointer %p cap: %d len:%d, which1: %s var pointer :%p arr pointer %p cap: %d len:%d\n",
which, &which, unsafe.Pointer(&which[0]), cap(which), len(which), which1, &which1, unsafe.Pointer(&which1[0]), cap(which1), len(which1))
which1 = append(which1, []byte("123")...)
fmt.Printf("which: %s var pointer :%p arr pointer %p cap: %d len:%d, which1: %s var pointer :%p arr pointer %p cap: %d len:%d\n",
which, &which, unsafe.Pointer(&which[0]), cap(which), len(which), which1, &which1, unsafe.Pointer(&which1[0]), cap(which1), len(which1))
}
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/18075905