-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers.go
executable file
·98 lines (82 loc) · 2.68 KB
/
pointers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Created on 23 Feb 2024
* @author Sai Sumanth
*/
package main
/*
# Memory Management, Pointers
--->> Resources <<---
1. https://www.udacity.com/blog/2021/05/c-pass-by-reference-explained.html
2. https://stackoverflow.com/questions/47296325/passing-by-reference-and-value-in-go-to-functions
3. https://dev.to/nikl/when-to-not-use-pointers-in-golang-kfi
*/
import "fmt"
func main() {
fmt.Println("Inside pointers.go")
var i int
j := 0
fmt.Printf("i: %v and is i == 0 ? %v\n", i, (i == 0)) // true
fmt.Printf("j: %v and is j == 0 ? %v\n", j, (j == 0)) // true
j = 90
// A pointer is a value that points to the memory address of another variable.
p := &j // here p is a pointer (contains address)
fmt.Printf("p value: %v\n", p)
fmt.Printf("p pointing to value : %v\n", *p)
*p = 99
fmt.Printf("j value: %v\n", j) // 99 j value got changed
counter := 0
// 6 secs timer
for i := 1; i <= 6; i++ {
fmt.Printf("counter: %v\n", counter)
// prints 0 everytime ever after calling incCounter method
// because when we are passing `counter` to [incCounter] copy is being created
// and copy is being modified
incCounter(counter)
}
fmt.Println("Timer Started again. This time it should work properly")
for i := 0; i < 6; i++ {
fmt.Printf("counter: %v\n", counter)
incCounterReal(&counter) // passing the address of counter variable
}
/*
new()
*/
intUsingNew := new(int)
fmt.Println("intUsingNew -> value will be address, since it's a pointer : ", intUsingNew, " and the data it points to:", *intUsingNew)
// 💡 just the type is integer pointer memory is not being allocated
var intUsingPointer *int
fmt.Printf("intUsingPointer: %v\n", intUsingPointer) // nil
// *intUsingPointer = 20 // 💀 we are dereferencing the nil pointer here. horribly goes wrong
num := 20
intUsingPointer = &num
// Now it's safe to dereference intUsingPointer since it points to a valid memory address
fmt.Printf("intUsingPointer: %v\n", *intUsingPointer) // 20
*intUsingPointer = 20
/*
-------> make() <-------
🌻 Make can be used only for creating maps, slices and channels 🌻
*/
sliceUsingMake := make([]int, 10, 15)
fmt.Println(sliceUsingMake) // [0 0 0 0 0 0 0 0 0 0]
/*
Pointers inside struct
*/
pointersWithStruct()
/*
💎 Does go supports Pass By Reference ?
*/
goHasOnlyPassByValueNoPassByRef()
/*
new() vs make()
*/
newVsMake()
}
func incCounter(val int) {
val++
}
func incCounterReal(c *int) {
// c contains the address of passed argument, copy gets created
*c++ // modifying the value using dereferencing pointer.
// btw this is not Pass by Reference. Go doesn't support Pass by ref unlike C++.
// Golang supports only Pass by Value
}