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
package main

import "fmt"

// 指针
// 区别于c/c++ Go语言指针不能进行偏移和运算 是安全指针
// 指针地址
// 指针类似
// 指针取值
// & 取地址 * 根据地址取值

// 指针传值例子
func mod1(x int) {
x = 100
}
func mod2(x *int) {
*x = 100
}
func modArry(a1 *[3]int) {
a1[0] = 20
}
// new 内置函数 接收一个类型参数 返回指向该类型的内存地址的指针
// func new(Type) *Type {

// }
// 得到一个int类型的指针
// var a = new(int)

// make 返回类型本身
// func make(t Type, size ...IntergerType) Type {

// }

func main() {
// a := 10
// b := &a
// fmt.Printf("a: %d ptr: %p\n", a, &a)
// fmt.Printf("b: %p type: %T\n", b, b)
// print(&b)
// print(*b)

a := 20
mod1(a)
fmt.Println(a)
mod2(&a)
fmt.Println(a)
a2 := [3]int{1, 2, 3}
modArry(&a2)
fmt.Println(a2)
}