数组扩容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))
}


posted @ 2024-03-15 17:32  vx_guanchaoguo0  阅读(9)  评论(0编辑  收藏  举报