Skip to content

Instantly share code, notes, and snippets.

@islishude
Created August 11, 2018 01:45
Show Gist options
  • Save islishude/0cf4b1a783c2f8b7aaa3f16334e1379d to your computer and use it in GitHub Desktop.
Save islishude/0cf4b1a783c2f8b7aaa3f16334e1379d to your computer and use it in GitHub Desktop.
Golang `slice` isn't point instead of a struct
package main
import "fmt"
func main() {
test := []int{0, 1, 2}
fmt.Printf("最初的内存地址是 %p 值为:%v \n", test, test)
change(test)
fmt.Printf("出函数的内存地址是 %p 值为:%v \n", test, test)
}
func change(param []int) {
fmt.Printf("进入函数后内存地址是 %p 值为:%v \n", param, param)
param = append(param, 3)
fmt.Printf("append后内存地址是 %p 值为:%v \n", param, param)
}
// 打印结果
// 最初的内存地址是 0xc0420640c0 值为:[0 1 2]
// 进入函数后内存地址是 0xc0420640c0 值为:[0 1 2]
// append后内存地址是 0xc042084030 值为:[0 1 2 3]
// 出函数的内存地址是 0xc0420640c0 值为:[0 1 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment