Go pointers
Go pointers
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
package main
import (
"fmt"
// "strconv"
// "math"
// "reflect"
// "net/http"
// "log"
)
func simple() {
a := 42
// b will be a brand new variable with it's place in memory
b := a
fmt.Printf("a: %v, b: %v\n", a, b)
a = 27
fmt.Printf("a: %v, b: %v\n", a, b)
}
func withPointer() {
var a int = 42
var b *int = &a
fmt.Printf("a: %v, b(memory location): %v, dereferencing: %v\n", a, b, *b)
a = 27
fmt.Printf("a: %v, b(memory location): %v, dereferencing: %v\n", a, b, *b)
}
func aritmwithPointer() {
a := [3]int{1, 2, 3}
b := &a[0]
c := &a[1]
fmt.Printf("a: %v, b: %v, c: %v\n", a, b, c)
fmt.Printf("a: %v, b: %v, c: %v\n", a, *b, *c)
fmt.Printf("a: %v, b: %p, c: %p\n", a, b, c)
var ms myStruct
ms = myStruct{foo: 42}
// var ms *myStruct
// ms = &myStruct{foo: 42}
fmt.Println(ms.foo)
}
type myStruct struct {
foo int
}
func main() {
// simple()
// withPointer()
aritmwithPointer()
}
This post is licensed under CC BY 4.0 by the author.
