GO语言练习:值与引用
1、代码
2、运行
1 package main 2 3 import "fmt" 4 5 func testValue(){ 6 fmt.Println("for value") 7 var a = [3]int {1, 2, 3} 8 var b = a 9 b[1]++ 10 fmt.Println("a = ",a, "\nb = ", b) 11 } 12 13 func testReference() { 14 fmt.Println("for reference") 15 var a = [3]int {1, 2, 3} 16 var b = &a 17 b[1]++ 18 fmt.Println("a = ",a, "\nb = ", *b) 19 } 20 21 func main() { 22 testValue() 23 testReference() 24 }
2、运行
$ go run valueAndRreference.go for value a = [1 2 3] b = [1 3 3] for reference a = [1 3 3] b = [1 3 3]
作 者:fengbohello
个人网站:http://www.fengbohello.top/
E-mail : fengbohello@foxmail.com
欢迎转载,转载请注明作者和出处。
因作者水平有限,不免出现遗漏和错误。希望热心的同学能够帮我指出来,我会尽快修改。愿大家共同进步,阿里嘎多~