-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (30 loc) · 1.07 KB
/
main.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
package main
func main() {
println("Pointers in golang")
/*
A pointer is simply a memory address of a value
We can reference a memory address by prefixing the variable/value by `&`
We can deference the value from the reference by `*`
*/
// Example
personName := "John doe"
var personRef *string = &personName
println("The person is present on", personRef, "in memory")
println("The value the personRef references", *personRef, "at", personRef, "memory address")
/*
A pointer makes sure that a ref of the memory is passed down the line
so that the actual value is modified/mutated/operated-on.
Most of the variables in Go are passed by value i.e the copies of actual
variables are passed which don't effect the actual variable
*/
changeName(personName)
println("The name didn't changes as it was passed as a copy:", personName)
changeNameByPointer(&personName)
println("The actual name changed as a ref was passed:", personName)
}
func changeName(name string) {
name = "Jane doe"
}
func changeNameByPointer(personName *string) {
*personName = "Jane Doe"
}