-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPointers.go
83 lines (63 loc) · 1.84 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
package main
import "fmt"
func main() {
numberValue := 42
numberPointer := &numberValue
fmt.Println("number", numberValue)
fmt.Println("pointer", numberPointer)
fmt.Println()
fmt.Println("Set number")
numberValue = 32
fmt.Println("number", numberValue)
fmt.Println("pointer", numberPointer)
fmt.Println()
fmt.Println("Set pointer value")
*numberPointer = 52
fmt.Println(numberValue)
fmt.Println("pointer", numberPointer)
fmt.Println()
fmt.Println(point{1, 2})
p := point{1, 2}
fmt.Println(p.X)
pointPointer := &p
pointPointer.X = 3
fmt.Println(p.X)
fmt.Println()
// Go is pass by value. Example shown below
answer := 42
fmt.Println("answer befor", &answer)
fmt.Println("answer befor", answer)
foo(answer)
fmt.Println("answer after", &answer)
fmt.Println("answer after", answer)
fmt.Println()
// Now with pointers
fmt.Println("answer befor", &answer)
fmt.Println("answer befor", answer)
fooPointer(&answer)
fmt.Println("answer after", &answer)
fmt.Println("answer after", answer)
/*
Can pointers negatively affect performance?
Absolutely. There are two major considerations here:
Dereferencing pointers isn’t free. It’s not a huge cost, but it can add up.
Sharing data via pointers will likely cause the data to be placed in the “heap.”
The heap is a section of memory for data that lives longer than a single function call.
There is overhead to adding data to the heap and heap data can only be cleaned up by the garbage collector.
The more data in the heap, the more work the garbage collector has to do, and the more impact it’ll have on your application.
*/
}
type point struct {
X int
Y int
}
func foo(x int) {
x = x * x
fmt.Println("answer updat", &x)
fmt.Println("answer updat", x)
}
func fooPointer(x *int) {
*x = *x * *x
fmt.Println("answer updat", x)
fmt.Println("answer updat", *x)
}