8
8
9
9
Go 语言的取地址符是 ` & ` ,放到一个变量前使用就会返回相应变量的内存地址。
10
10
11
- 下面的代码片段(Example 4.9 [ pointer.go] ( examples/chapter_4/pointer.go ) )可能输出 ` An integer: 5, its location in memory: 0x6b0820 ` (这个值随着你每次运行程序而变化)。
12
-
13
- var i1 = 5
14
- fmt.Printf("An integer: %d, it's location in memory: %p\n", i1, &i1)
11
+ 下面的代码片段(示例 4.9 [ pointer.go] ( examples/chapter_4/pointer.go ) )可能输出 ` An integer: 5, its location in memory: 0x6b0820 ` (这个值随着你每次运行程序而变化)。
12
+
13
+ ``` go
14
+ var i1 = 5
15
+ fmt.Printf (" An integer: %d , it's location in memory: %p \n " , i1, &i1)
16
+ ```
15
17
16
18
这个地址可以存储在一个叫做指针的特殊数据类型中,在本例中这是一个指向 int 的指针,即 ` i1 ` :此处使用 * int 表示。如果我们想调用指针 intP,我们可以这样声明它:
17
-
18
- var intP *int
19
+
20
+ ``` go
21
+ var intP *int
22
+ ```
19
23
20
24
然后使用 ` intP = &i1 ` 是合法的,此时 intP 指向 i1。
21
25
@@ -39,20 +43,22 @@ intP 存储了 i1 的内存地址;它指向了 i1 的位置,它引用了变
39
43
40
44
现在,我们应当能理解 pointer.go 中的整个程序和他的输出:
41
45
42
- Example 4.21 [ pointer.go] ( examples/chapter_4/pointer.go ) :
43
-
44
- package main
45
- import "fmt"
46
- func main() {
47
- var i1 = 5
48
- fmt.Printf("An integer: %d, its location in memory: %p\n", i1, &i1)
49
- var intP *int
50
- intP = &i1
51
- fmt.Printf("The value at memory location %p is %d\n", intP, *intP)
52
- }
46
+ 示例 4.21 [ pointer.go] ( examples/chapter_4/pointer.go ) :
47
+
48
+ ``` go
49
+ package main
50
+ import " fmt"
51
+ func main () {
52
+ var i1 = 5
53
+ fmt.Printf (" An integer: %d , its location in memory: %p \n " , i1, &i1)
54
+ var intP *int
55
+ intP = &i1
56
+ fmt.Printf (" The value at memory location %p is %d \n " , intP, *intP)
57
+ }
58
+ ```
53
59
54
60
输出:
55
-
61
+
56
62
An integer: 5, its location in memory: 0x24f0820
57
63
The value at memory location 0x24f0820 is 5
58
64
@@ -64,18 +70,20 @@ Example 4.21 [pointer.go](examples/chapter_4/pointer.go):
64
70
65
71
它展示了分配一个新的值给 * p 并且更改这个变量自己的值(这里是一个字符串)。
66
72
67
- Example 4.22 [ string_pointer.go] ( examples/chapter_4/string_pointer.go )
68
-
69
- package main
70
- import "fmt"
71
- func main() {
72
- s := "good bye"
73
- var p *string = &s
74
- *p = "ciao"
75
- fmt.Printf("Here is the pointer p: %p\n", p) // prints address
76
- fmt.Printf("Here is the string *p: %s\n", *p) // prints string
77
- fmt.Printf("Here is the string s: %s\n", s) // prints same string
78
- }
73
+ 示例 4.22 [ string_pointer.go] ( examples/chapter_4/string_pointer.go )
74
+
75
+ ``` go
76
+ package main
77
+ import " fmt"
78
+ func main () {
79
+ s := " good bye"
80
+ var p *string = &s
81
+ *p = " ciao"
82
+ fmt.Printf (" Here is the pointer p: %p \n " , p) // prints address
83
+ fmt.Printf (" Here is the string *p: %s \n " , *p) // prints string
84
+ fmt.Printf (" Here is the string s: %s \n " , s) // prints same string
85
+ }
86
+ ```
79
87
80
88
输出:
81
89
@@ -92,10 +100,12 @@ Example 4.22 [string_pointer.go](examples/chapter_4/string_pointer.go)
92
100
** 注意事项**
93
101
94
102
你不能得到一个文字或常量的地址,例如:
95
-
96
- const i = 5
97
- ptr := &i //error: cannot take the address of i
98
- ptr2 := &10 //error: cannot take the address of 10
103
+
104
+ ``` go
105
+ const i = 5
106
+ ptr := &i // error: cannot take the address of i
107
+ ptr2 := &10 // error: cannot take the address of 10
108
+ ```
99
109
100
110
所以说,Go 语言和 C、C++ 以及 D 语言这些低级(系统)语言一样,都有指针的概念。但是对于经常导致 C 语言内存泄漏继而程序崩溃的指针运算(所谓的指针算法,如:` pointer+2 ` ,移动指针指向字符串的字节数或数组的某个位置)是不被允许的。Go 语言中的指针保证了内存安全,更像是 Java、C# 和 VB.NET 中的引用。
101
111
@@ -111,15 +121,17 @@ Example 4.22 [string_pointer.go](examples/chapter_4/string_pointer.go)
111
121
112
122
对一个空指针的反向引用是不合法的,并且会使程序崩溃:
113
123
114
- Example 4.23 [ testcrash.go] ( examples/chapter_4/testcrash.go ) :
115
-
116
- package main
117
- func main() {
118
- var p *int = nil
119
- *p = 0
120
- }
121
- // in Windows: stops only with: <exit code="-1073741819" msg="process crashed"/>
122
- // runtime error: invalid memory address or nil pointer dereference
124
+ 示例 4.23 [ testcrash.go] ( examples/chapter_4/testcrash.go ) :
125
+
126
+ ``` go
127
+ package main
128
+ func main () {
129
+ var p *int = nil
130
+ *p = 0
131
+ }
132
+ // in Windows: stops only with: <exit code="-1073741819" msg="process crashed"/>
133
+ // runtime error: invalid memory address or nil pointer dereference
134
+ ```
123
135
124
136
** 问题 4.2** 列举 Go 语言中 * 号的所有用法。
125
137
0 commit comments