-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointers.go
40 lines (31 loc) · 930 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
import "fmt"
func main() {
star := "Polaris"
var starAddress *string = &star
fmt.Println("The address of star is", starAddress)
*starAddress = "Sirius"
fmt.Println("The value of star is", star)
// changing values with different scopes with pointers/references
// take the global variable:
var x int = 10
// we won't be able to change the value of x in the main function
addHundredByValue(x)
fmt.Println("Value of x after addHundredByValue:", x)
// but we can change the value of x in the main function by passing a pointer to x
addHundredByReference(&x)
fmt.Println("Value of x after addHundredByReference:", x)
// another example:
greeting := "Hello there!"
brainwash(&greeting)
fmt.Println("greeting is now:", greeting)
}
func addHundredByValue(num int) {
num += 100
}
func addHundredByReference(numPtr *int) {
*numPtr += 100
}
func brainwash(saying *string) {
*saying = "Beep Boop"
}