You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/pointers/about.md
+11-5Lines changed: 11 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,18 +31,18 @@ The piece of memory that is associated with `a` will now will now be storing the
31
31
32
32
## Pointers
33
33
34
-
While variables allow us to refer to values in memory, sometimes it's useful to know the **memory address** to which the variable is pointing. **Pointers** hold the memory addresses of those values. A pointer declaration looks like this:
34
+
While variables allow us to refer to values in memory, sometimes it's useful to know the **memory address** to which the variable is pointing. **Pointers** hold the memory addresses of those values. You declare a variable with a pointer type by prefixing the underlying type with an asterisk:
35
35
36
36
```go
37
37
varp *int// 'p' contains the memory address of an integer
38
38
```
39
39
40
-
Here we declaring a variable `p` of type `*int`. The type `*int` means "pointer to int". In other words, we are saying that `p` is a pointer to int, which means that `p` will hold the memory address of an integer. The zero value of pointers is `nil` because a `nil` pointer holds no memory address.
40
+
Here we declaring a variable `p` of type "pointer to int" (`*int`). This means that `p` will hold the memory address of an integer. The zero value of pointers is `nil` because a `nil` pointer holds no memory address.
41
41
42
42
### Getting a pointer to a variable
43
43
44
44
To find the memory address of the value of a variable, we can use the `&` operator.
45
-
For example, if we want to find and store the member address of variable `a` in the pointer `p`, we can do the following:
45
+
For example, if we want to find and store the memory address of variable `a` in the pointer `p`, we can do the following:
46
46
47
47
```go
48
48
varaint
@@ -54,7 +54,7 @@ p = &a // the variable 'p' will hold the memory address of 'a'
54
54
55
55
### Accessing the value via a pointer (dereferencing)
56
56
57
-
When we have a pointer, we might want to know the value stored in the memory address to which it points. We can do this using the `*` operator:
57
+
When we have a pointer, we might want to know the value stored in the memory address the pointer represents. We can do this using the `*` operator:
58
58
59
59
```go
60
60
varaint
@@ -87,6 +87,11 @@ Assigning to `*pa` will change the value stored at the memory address `pa` holds
87
87
88
88
A note of caution however: always check if a pointer is not `nil` before dereferencing. Dereferencing a `nil` pointer will make the program crash at runtime!
So far we've only seen pointers to primitive values. We can also create pointers for structs:
@@ -143,9 +148,10 @@ fmt.Println(ages)
143
148
// The changes the function 'addPeterAge' made to the map are visible after the function ends!
144
149
```
145
150
151
+
The same applies when changing an existing item in a slice. However, actions that return a new slice like `append` will **not** modify the slice outside of the function.
146
152
## Pointer arithmetic
147
153
148
-
Unlike other languages, pointer arithmetic is not allowed in Go. This means that snippets like the following are invalid:
154
+
Unlike other languages, Go does not support pointer arithmetic. This means you cannot go from a memory address of one value to the address of another by incrementing the pointer value. So snippets like the following are invalid:
0 commit comments