Go support pointers, allowing you to pass references to values and records within your program
package main import ( "fmt" ) func zeroval(ival int) { ival = 0 } func zeroptr(iptr *int) { *iptr = 0 } func main() { i := 1 fmt.Println("initial : ", i) zeroval(i) fmt.Println("zeroval : ", i) zeroptr(&i) fmt.Println("zeroptr : ", i) fmt.Println("pointer : ", &i) }
initial : 1 zeroval : 1 zeroptr : 0 pointer : 0x208178170
总结 :
1 : 传指针的话能直接改变主函数里面的值
时间: 2024-10-01 03:52:34