Value Ptr
###value & ptr
func checkValuePtr() {
one := [2]int{1, 2} // pass by value
two := one // value copy ?! what Go compiler do !?
// one[1] = 7 // or change copy
// fmt.Println(one)
// fmt.Println(two)
//fmt.Println(&one)
//fmt.Println(&two)
fmt.Println(&one[0])
fmt.Println(&two[0])
}
Result below :
[ ~/Code/git/gopher/src/hello/ ] #
[ replay
| done: 18.21721ms ]
[1 7]
[1 2]
[ ~/Code/git/gopher/src/hello/ ] #
[ replay
| done: 18.169683ms ]
[1 2]
[1 2]
[ ~/Code/git/gopher/src/hello/ ] #
[ replay
| done: 18.309969ms ]
&[1 2]
&[1 2]
[ ~/Code/git/gopher/src/hello/ ] #
[ replay
| done: 18.196578ms ]
0x11200448
0x11200450
##conclusion
- Pass by value do
Copy
original value ! @1 - Pass by ptr do
Copy
pointer but original invariable ! @2 - @1 happened at
Receiver
、Param pass
、assignment
…