/
pointer_parameter.go
53 lines (42 loc) · 1.27 KB
/
pointer_parameter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import "fmt"
func main() {
/*
指针作为参数:
参数的传递:值传递,引用传递
不管是作为参数还是作为返回值,使用指针可以节省内存
否则值传递会将数据备份一份,会多消耗一份内存
*/
a := 10
fmt.Println("fun1()函数调用前,a:", a)
fun1(a)
fmt.Println("fun1()函数调用后,a:", a)
fun2(&a)
fmt.Println("fun2()函数调用后,a:", a)
arr1 := [4]int{1, 2, 3, 4}
fmt.Println("fun3()函数调用前:", arr1)
fun3(arr1)
fmt.Println("fun3()函数调用后:", arr1)
fun4(&arr1)
fmt.Println("fun4()函数调用后:", arr1)
}
func fun4(p2 *[4]int) { // 引用传递
fmt.Println("fun4()函数中的数组指针:", p2)
p2[0] = 200
fmt.Println("fun4()函数中的数组指针:", p2)
}
func fun3(arr2 [4]int) { // 值传递
fmt.Println("fun3()函数中数组的:", arr2)
arr2[0] = 100
fmt.Println("fun3()函数中修改数组:", arr2)
}
func fun2(p1 *int) { //传递的是a的地址,就是引用传递
fmt.Println("fun2()函数中,p1:", *p1)
*p1 = 200
fmt.Println("fun2()函数中,修改p1:", *p1)
}
func fun1(num int) { // 值传递:num = a = 10
fmt.Println("fun1()函数中,num的值:", num)
num = 100
fmt.Println("fun1()函数中修改num:", num)
}