Slide 1

Slide 1 text

HELLO, MY NAME IS GEORGI @GeorgiCodes

Slide 2

Slide 2 text

array declaration var elements [4]int

Slide 3

Slide 3 text

func main() { a := [4]int{1, 2, 3, 4} println("a addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } }

Slide 4

Slide 4 text

on 64bit architecture, each int = 8 bytes func main() { a := [4]int{1, 2, 3, 4} println("a addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } a addr: 0x220822bf20 Value: 1 IndexAddr: 0x220822bf20 Value: 2 IndexAddr: 0x220822bf28 Value: 3 IndexAddr: 0x220822bf30 Value: 4 IndexAddr: 0x220822bf38

Slide 5

Slide 5 text

arrays are contiguous in memory

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

CPU cache

Slide 8

Slide 8 text

Pass by value

Slide 9

Slide 9 text

In Go, everything is pass by value. When we pass an array as an argument, we pass a copy of the array not a reference to the array. package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" }

Slide 10

Slide 10 text

1. A copy of names is made when f1 is called 1 package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" }

Slide 11

Slide 11 text

2. The copy of names is assigned to local var a 1 package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" } 2

Slide 12

Slide 12 text

3. We make a change to the copy 3 2 1 package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" }

Slide 13

Slide 13 text

3 2 1 package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(names) println(names[0]) } func f1(a [4]string) { a[0] = "marie" } // ada

Slide 14

Slide 14 text

call stack

Slide 15

Slide 15 text

Pointers

Slide 16

Slide 16 text

If we want to share names with f1 so f1 can modify it, then we pass the address of names to f1. package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" }

Slide 17

Slide 17 text

1. Pass the address of names package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 1

Slide 18

Slide 18 text

2. f1 accepts a pointer variable package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 2 1

Slide 19

Slide 19 text

3. Changes to a update the array that a points to, names. package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 3 2 1

Slide 20

Slide 20 text

package main func main() { names := [4]string {"ada", "lovelace", "tom", "jerry"} f1(&names) println(names[0]) } func f1(a *[4]string) { a[0] = "marie" } 3 2 1 // marie

Slide 21

Slide 21 text

call stack using a pointer

Slide 22

Slide 22 text

the Go compiler likes to optimise all the things!

Slide 23

Slide 23 text

func main() { a := [4]int{1, 2, 3, 4} println("a addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } print

Slide 24

Slide 24 text

func main() { a := [4]int{1, 2, 3, 4} println("a addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } print func main() { a := [4]int{1, 2, 3, 4} println("a addr:", &a) for i := range a { print("Value: ", a[i]) println(" Address:", &a[i]) } } import "fmt" func main() { a := [4]int{1, 2, 3, 4} fmt.Printf("a addr: %p \n", &a) for i := range a { fmt.Printf("V: %v A: %p \n", a[i], &a[i]) } } import "fmt" func main() { a := [4]int{1, 2, 3, 4} fmt.Printf("a addr: %p \n", &a) for i := range a { fmt.Printf("V: %v A: %p \n", a[i], &a[i]) } } fmt.Print

Slide 25

Slide 25 text

1. escape analysis go build -gcflags=-m main.go:6: moved to heap: a main.go:8: &a escapes to heap main.go:11: &a[i] escapes to heap main.go:8: main ... argument does not escape main.go:11: main ... argument does not escape with ft.Printf

Slide 26

Slide 26 text

1. inlining func main() { names := [2]string{"ada", "lovelace"} f1(names) println(names[0]) } func f1(a [2]string) { a[0] = "marie" }

Slide 27

Slide 27 text

inlining func main() { names := [2]string{"ada", "lovelace"} f1(names) println(names[0]) } func f1(a [2]string) { a[0] = "marie" // Do this to prevent inlining. var x int fmt.Sprintf("Prevent Inlining: %d", x) }

Slide 28

Slide 28 text

thank-you! @GeorgiCodes georgi.io