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]
有疑问加站长微信联系(非本文作者)