-
Notifications
You must be signed in to change notification settings - Fork 86
/
pointers.go
40 lines (30 loc) · 849 Bytes
/
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
package main
func decr(number *int) {
*number--
}
func main() {
// simple integer
number := 42
// point to a number
var numberPointer *int
// while not initialized numberPointer is null
if numberPointer == nil {
println("initialize null")
}
// assign the adress of number to numberPointer
// "&" means "address of"
numberPointer = &number
// "*" means "value pointed by"
println(*numberPointer)
// without asterisk will print the adress pointed by numberPointer(address of number)
println(numberPointer)
// if changes the value of number
number++
// numberPointer value also changes(are the same value - memory address)
println(number)
println(*numberPointer)
// pointer can also be used to change the value of a variable inside a function
// pass "address of" number as parameter
decr(&number)
println(number)
}