Skip to content

Commit 0c75328

Browse files
committed
Tutorial7 - Pointers
1 parent b76c112 commit 0c75328

3 files changed

Lines changed: 143 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ go run tutorial1/main.go
4848
- [x] [Arrays, Slices, Maps and Loops](tutorial4)
4949
- [x] [Strings, Runes, and UTF-8 Encoding](tutorial5)
5050
- [x] [Structs and Interfaces](tutorial6)
51+
- [x] [Pointers](tutorial7)

tutorial7/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Pointers in Go
2+
3+
A pointer is a variable that stores the memory address of another variable. In Go, a pointer is represented using the `*` (asterisk) followed by the type of the stored value.
4+
5+
## Creating Pointers
6+
7+
You can create a pointer using the built-in `new` function:
8+
9+
```go
10+
var p *int32 = new(int32) // p now stores a memory location
11+
```
12+
13+
This creates a new `int32` value, sets it to zero (the zero value for integers), and returns a pointer to it.
14+
15+
## Using Pointers
16+
17+
You can change the value stored at the memory location a pointer points to:
18+
19+
```go
20+
*p = 1 // changes the value at the memory location p points to
21+
```
22+
23+
You can also create a pointer from the address of another variable using the `&` operator:
24+
25+
```go
26+
p = &i // p and i now reference the same int32 value in memory
27+
```
28+
29+
## Pointers and Arrays
30+
31+
When working with arrays, you can pass a pointer to the array to a function if you want the function to be able to modify the original array:
32+
33+
```go
34+
func square(thing2 *[5]float64) [5]float64{
35+
for i := range thing2{
36+
thing2[i] = thing2[i]*thing2[i]
37+
}
38+
return *thing2
39+
}
40+
```
41+
42+
In this function, `thing2` is a pointer to an array of `float64`. The function squares each element of the array. Because we're passing a pointer to the array, the original array is modified.
43+
44+
### Checkout the code
45+
46+
- [main.go](main.go)

tutorial7/main.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
//var p *int32 ---
7+
//variable p will hold memory address of an int32
8+
// but this pointer doesn't point to anything yet it's value is nil yet
9+
// in other words this pointer does not have an address assigned to it
10+
11+
// to give pointer an address we will use builtin new function
12+
13+
var p *int32 = new(int32) //now p stores a memory location
14+
var i int32
15+
16+
fmt.Printf("The value p points to is: %v" , *p)
17+
fmt.Printf("\nThe value if i is: %v",i) // initially they both will be zero value no value assigned
18+
19+
// to change the values stored at memory location
20+
// make sure your pointer isn't nil before assigning
21+
// *p = 10
22+
23+
// we can also create a pointer from the address of another varialble using '&'
24+
// p = &i now they both reference the same int32 value in memory
25+
26+
p = &i
27+
*p = 1 // here value of i will also changes
28+
fmt.Printf("\nThe value p points after to is: %v" , *p)
29+
fmt.Printf("\nThe value after if i is: %v",i)
30+
31+
/*
32+
If we use with regular variable
33+
var k int32 = 2
34+
i = k //the value of i also changes
35+
36+
The main exception of this copy behaviour of non-pointer variables is when working with slices
37+
*/
38+
var slice = []int32{1,2,3}
39+
var sliceCopy = slice
40+
sliceCopy[2] = 4
41+
fmt.Printf("\nOG slice: %v", slice)
42+
fmt.Printf("\ncopy of slice: %v", sliceCopy)
43+
/*
44+
in terminal we will see value of original slice also changes
45+
because under the hood slices contains pointer to underlying array
46+
*/
47+
48+
var thing1 = [5]float64{1,2,3,4,5}
49+
fmt.Printf("\nThe memory location of the thing1 array is: %p",&thing1)
50+
//var result [5]float64 = square(thing1) this in case of copy
51+
var result [5]float64 = square(&thing1)
52+
fmt.Printf("\nThe result is: %v",result)
53+
fmt.Printf("\nThe value of thing1 is: %v", thing1)
54+
}
55+
56+
func square(thing2 *[5]float64) [5]float64{
57+
fmt.Printf("\nThe memory location of the thing2 array is: %p",&thing2)
58+
for i := range thing2{
59+
thing2[i] = thing2[i]*thing2[i]
60+
}
61+
return *thing2
62+
}
63+
64+
/*
65+
func square(thing2 [5]float64) [5]float64{
66+
fmt.Printf("\nThe memory location of the thing2 array is: %p",&thing2)
67+
for i := range thing2{
68+
thing2[i] = thing2[i]*thing2[i]
69+
}
70+
return thing2
71+
}
72+
the memory addresses of thing1 and thing2 are different means these are 2 different arrays
73+
that means we can modify the values of thing2 without effecting the thing1
74+
75+
currently in square we are doubling the memory usage, because we are creating a copy for use in function
76+
we using way more memory then we need instead we will be using pointers
77+
we can make our function taking a pointer to an array instead the memory location will be same
78+
*/
79+
80+
81+
/*
82+
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> go run tutorial7/main.go
83+
84+
O/P ::::::::::::::::::::::::
85+
86+
The value p points to is: 0
87+
The value if i is: 0
88+
The value p points after to is: 1
89+
The value after if i is: 1
90+
OG slice: [1 2 4]
91+
copy of slice: [1 2 4]
92+
The memory location of the thing1 array is: 0xc00000e450
93+
The memory location of the thing2 array is: 0xc00005a030
94+
The result is: [1 4 9 16 25]
95+
The value of thing1 is: [1 4 9 16 25]
96+
*/

0 commit comments

Comments
 (0)