|  | 
| 1 | 1 | # About | 
| 2 | 2 | 
 | 
| 3 |  | -TODO: add information on pointers concept | 
|  | 3 | +Like many other languages, Go has pointers.  | 
|  | 4 | +If you're new to pointers, they can feel a little mysterious but once you get used to them, they're quite straight-forward. | 
|  | 5 | +They're a crucial part of Go, so take some time to really understand them. | 
|  | 6 | + | 
|  | 7 | +Before digging into the details, it's worth understanding the use of pointers. Pointers are a way to share memory with other parts of our program, which is useful for two major reasons: | 
|  | 8 | +1. When we have large amounts of data, making copies to pass between functions is very inefficient.  | 
|  | 9 | +  By passing the memory location of where the data is stored instead, we can dramatically reduce the resource-footprint of our programs. | 
|  | 10 | +2. By passing pointers between functions, we can access and modify the computer's memory directly, meaning that any changes made by one function are immediately visible to other parts of the program when the function ends. | 
|  | 11 | + | 
|  | 12 | +## Variables and Memory | 
|  | 13 | + | 
|  | 14 | +Let's say we have a regular integer variable `a`: | 
|  | 15 | + | 
|  | 16 | +```go | 
|  | 17 | +var a int | 
|  | 18 | +``` | 
|  | 19 | + | 
|  | 20 | +When we declare a variable, Go has to find a place in memory to store its value. This is largely abstracted from us — when we need to fetch the value stored in that piece of memory, we can just refer to it by the variable name. | 
|  | 21 | + | 
|  | 22 | +For instance, when we write `a + 2`, we are effectively fetching the value stored in the memory associated with the variable `a` and adding 2 to it. | 
|  | 23 | + | 
|  | 24 | +Similarly, when we need to change the value in the piece of memory of `a`, we can use the variable name to do an assignment: | 
|  | 25 | + | 
|  | 26 | +```go | 
|  | 27 | +a = 3 | 
|  | 28 | +``` | 
|  | 29 | + | 
|  | 30 | +The piece of memory that is associated with `a` will now will now be storing the value `3`. | 
|  | 31 | + | 
|  | 32 | +## Pointers | 
|  | 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: | 
|  | 35 | + | 
|  | 36 | +```go | 
|  | 37 | +var p *int // 'p' contains the memory address of an integer | 
|  | 38 | +``` | 
|  | 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. | 
|  | 41 | + | 
|  | 42 | +### Getting a pointer to a variable | 
|  | 43 | + | 
|  | 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: | 
|  | 46 | + | 
|  | 47 | +```go | 
|  | 48 | +var a int | 
|  | 49 | +a = 2 | 
|  | 50 | + | 
|  | 51 | +var p *int | 
|  | 52 | +p = &a // the variable 'p' will hold the memory address of 'a' | 
|  | 53 | +``` | 
|  | 54 | + | 
|  | 55 | +### Accessing the value via a pointer (dereferencing) | 
|  | 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: | 
|  | 58 | + | 
|  | 59 | +```go | 
|  | 60 | +var a int  | 
|  | 61 | +a = 2 | 
|  | 62 | + | 
|  | 63 | +var p *int  | 
|  | 64 | +p = &a // the variable 'p' will hold the memory address of 'a' | 
|  | 65 | + | 
|  | 66 | +var b int | 
|  | 67 | +b = *p // b == 2 | 
|  | 68 | +``` | 
|  | 69 | + | 
|  | 70 | +The operation `*p` fetches the value stored at the memory address stored in `p`. This operation is often called "dereferencing". | 
|  | 71 | + | 
|  | 72 | +We can also use the derefering operator to assign a new value to the memory address referenced by the pointer: | 
|  | 73 | + | 
|  | 74 | +```go | 
|  | 75 | +var a int | 
|  | 76 | +a = 2   // declare int variable 'a' and assign it the value of 2 | 
|  | 77 | + | 
|  | 78 | +var pa *int | 
|  | 79 | +pa = &a          // 'pa' now refers to the memory address of 'a'   | 
|  | 80 | +*pa = *pa + 2    // increment by 2 the value at memory address 'pa' | 
|  | 81 | + | 
|  | 82 | +fmt.Println(a)   // Output: 4 | 
|  | 83 | +                 // 'a' will have the new value that was changed via the pointer! | 
|  | 84 | +``` | 
|  | 85 | + | 
|  | 86 | +Assigning to `*pa` will change the value stored at the memory address `pa` holds. Since `pa` holds the memory address of `a`, by assigning to `*pa` we are effectively changing the value of `a`!  | 
|  | 87 | + | 
|  | 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! | 
|  | 89 | + | 
|  | 90 | +### Pointers to structs | 
|  | 91 | + | 
|  | 92 | +So far we've only seen pointers to primitive values. We can also create pointers for structs: | 
|  | 93 | + | 
|  | 94 | +```go | 
|  | 95 | +type Person struct { | 
|  | 96 | +    Name string | 
|  | 97 | +    Age  int | 
|  | 98 | +} | 
|  | 99 | + | 
|  | 100 | +var peter Person | 
|  | 101 | +peter = Person{Name: "Peter", Age: 22} | 
|  | 102 | + | 
|  | 103 | +var p *Person | 
|  | 104 | +p = &peter | 
|  | 105 | +``` | 
|  | 106 | + | 
|  | 107 | +We could have also created a new `Person` and immediately stored a pointer to it: | 
|  | 108 | + | 
|  | 109 | +```go | 
|  | 110 | +var p *Person | 
|  | 111 | +p = &Person{Name: "Peter", Age: 22} | 
|  | 112 | +``` | 
|  | 113 | + | 
|  | 114 | +When we have a pointer to a struct, we don't need to dereference the pointer before accessing one of the fields: | 
|  | 115 | + | 
|  | 116 | +```go | 
|  | 117 | +var p *Person | 
|  | 118 | +p = &Person{Name: "Peter", Age: 22} | 
|  | 119 | + | 
|  | 120 | +fmt.Println(p.Name) // Output: "Peter"  | 
|  | 121 | +                    // Go automatically dereferences 'person' to allow | 
|  | 122 | +                    // access to the 'Name' field | 
|  | 123 | +``` | 
|  | 124 | + | 
|  | 125 | +## Slices and maps are already pointers | 
|  | 126 | + | 
|  | 127 | +Slices and maps are special types because they already have pointers in their implementation. This means that more often that not, we don't need to create pointers for these types to share the memory address for their values. Imagine we have a function that adds a key to a map: | 
|  | 128 | + | 
|  | 129 | + | 
|  | 130 | +```go | 
|  | 131 | +func addPeterAge(m map[string]int){ | 
|  | 132 | +	m["Peter"] = 22 | 
|  | 133 | +} | 
|  | 134 | +``` | 
|  | 135 | + | 
|  | 136 | +If we create a map and call this function, the changes the function made to the map persist after the function ended. This is a similar behavior we get if we were using a pointer, but note how on this example we are not using any referencing/dereferencing or any of the pointer syntax: | 
|  | 137 | + | 
|  | 138 | +```go | 
|  | 139 | +ages := make(map[string]int) | 
|  | 140 | +addPeterAge(ages) | 
|  | 141 | +fmt.Println(ages) | 
|  | 142 | +// Output: map[Peter:22] | 
|  | 143 | +// The changes the function 'addPeterAge' made to the map are visible after the function ends! | 
|  | 144 | +``` | 
|  | 145 | + | 
|  | 146 | +## Pointer arithmetic | 
|  | 147 | + | 
|  | 148 | +Unlike other languages, pointer arithmetic is not allowed in Go. This means that snippets like the following are invalid: | 
|  | 149 | + | 
|  | 150 | +```go | 
|  | 151 | +a := 2 | 
|  | 152 | +pa := &a | 
|  | 153 | +pa++ // NOT ALLOWED: incrementing the pointer or any other pointer arithmetic operations are invalid | 
|  | 154 | +``` | 
0 commit comments