Slide 1

Slide 1 text

understanding nil @francesc

Slide 2

Slide 2 text

thanks

Slide 3

Slide 3 text

welcome every single one of you

Slide 4

Slide 4 text

agenda what is nil? what is nil in Go? what does nil mean? is nil useful?

Slide 5

Slide 5 text

nil? you misspelled null

Slide 6

Slide 6 text

how I learn words

Slide 7

Slide 7 text

how I learn words

Slide 8

Slide 8 text

etymology nil Latin nihil meaning nothing null Latin ne + ullus meaning not any none Old English ne + ān meaning not one

Slide 9

Slide 9 text

names for the number zero in English - zero - cipher - null - love - duck - nil - nada - zilch - zip - the letter ‘o’ - naught - nought - aught - ought source: wikipedia

Slide 10

Slide 10 text

nil is (a) zero

Slide 11

Slide 11 text

interlude a bit of history

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

“I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language.” - Sir C.A.R. Hoare

Slide 14

Slide 14 text

panic: runtime error: invalid memory address or nil pointer dereference

Slide 15

Slide 15 text

Uncaught TypeError: undefined is not a function

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

nil leads to panic

Slide 18

Slide 18 text

panic leads to fear by danpawley on Flickr

Slide 19

Slide 19 text

fear leads to by korymatthew on Flickr

Slide 20

Slide 20 text

λ functional programming

Slide 21

Slide 21 text

“Functional Go? Thanks, but no” - francesc at dotGo.eu 2015

Slide 22

Slide 22 text

there are many ways, nil is the Go way

Slide 23

Slide 23 text

Slide 24

Slide 24 text

zero values what are they?

Slide 25

Slide 25 text

bool → false numbers→ 0 string → "" zero values pointers → nil slices → nil maps → nil channels → nil functions → nil interfaces → nil

Slide 26

Slide 26 text

zero values for struct types type Person struct { AgeYears int Name string Friend []Person } var p Person // Person{0, "", nil}

Slide 27

Slide 27 text

the type of nil

Slide 28

Slide 28 text

“... unless the value is the predeclared identifier nil, which has no type” - Go language specification

Slide 29

Slide 29 text

untyped zero a := false a := "" a := 0 // a := int(0) a := 0.0 // a := float64(0) a := nil // use of untyped nil

Slide 30

Slide 30 text

“nil is a predeclared identifier representing the zero value for a pointer, channel, func, interface, map, or slice type.” - Go documentation for “builtin” package

Slide 31

Slide 31 text

“nil is a predeclared identifier representing the zero value for a pointer, channel, func, interface, map, or slice type.” - Go documentation for “builtin” package

Slide 32

Slide 32 text

twenty-five* keywords break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var * plus the five secret ones, obviously

Slide 33

Slide 33 text

// YOLO var nil = errors.New(“¯\_(ツ)_/¯”) * For extra evilness: place at the end of doc.go predefined vs keyword

Slide 34

Slide 34 text

zero values what do they mean?

Slide 35

Slide 35 text

pointers slices maps channels functions interfaces kinds of nil

Slide 36

Slide 36 text

pointers slices maps channels functions interfaces kinds of nil

Slide 37

Slide 37 text

- they point to a position in memory - similar to C or C++, but - no pointer arithmetic → memory safety - garbage collection pointers in Go

Slide 38

Slide 38 text

- points to nil a.k.a. nothing - zero value of pointers nil pointer

Slide 39

Slide 39 text

pointers slices maps channels functions interfaces kinds of nil

Slide 40

Slide 40 text

slice internals ptr len cap *elem int int []byte

Slide 41

Slide 41 text

a slice with five elements s := make([]byte, 5) 5 5 0 0 0 0 0 []byte [5]byte ptr len cap

Slide 42

Slide 42 text

nil slice var s []byte nil 0 0 []byte ptr len cap

Slide 43

Slide 43 text

pointers slices maps channels functions interfaces kinds of nil

Slide 44

Slide 44 text

channels, maps, and functions ptr *something

Slide 45

Slide 45 text

channels, maps, and functions ptr implementation * implementations might not be cloud shaped

Slide 46

Slide 46 text

channels, maps, and functions ptr nil

Slide 47

Slide 47 text

pointers slices maps channels functions interfaces kinds of nil

Slide 48

Slide 48 text

(type, value)

Slide 49

Slide 49 text

var s fmt.Stringer // Stringer (nil, nil) fmt.Println(s == nil) // true

Slide 50

Slide 50 text

(nil, nil) equals nil

Slide 51

Slide 51 text

var p *Person // nil of type *Person var s fmt.Stringer = p // Stringer (*Person, nil) fmt.Println(s == nil) // false

Slide 52

Slide 52 text

doesn’t equal nil (*Person, nil)

Slide 53

Slide 53 text

when is nil not nil?

Slide 54

Slide 54 text

func do() error { var err *doError return err } func main() { err := do() fmt.Println(err == nil) } when is nil not nil? // nil of type *doError // error (*doError, nil) // error (*doError, nil) // false

Slide 55

Slide 55 text

do not declare concrete error vars

Slide 56

Slide 56 text

func do() *doError { return nil } func main() { err := do() fmt.Println(err == nil) } // nil of type *doError // nil of type *doError // true nil is not nil

Slide 57

Slide 57 text

func do() *doError { return nil } func wrapDo() error { return do() } func main() { err := wrapDo() fmt.Println(err == nil) } nil is not nil // nil of type *doError // error (*doError, nil) // nil of type *doError // error (*doError, nil) // false

Slide 58

Slide 58 text

do not return concrete error types

Slide 59

Slide 59 text

* for some kinds of nil nil is not nil

Slide 60

Slide 60 text

pointers point to nothing slices have no backing array maps are not initialized channels are not initialized functions are not initialized interfaces have no value assigned, not even a nil pointer kinds of nil

Slide 61

Slide 61 text

“Make the zero value useful” - Rob Pike in his Go Proverbs

Slide 62

Slide 62 text

pointers slices maps channels functions interfaces how are these useful?

Slide 63

Slide 63 text

pointers slices maps channels functions interfaces how are these useful?

Slide 64

Slide 64 text

var p *int p == nil // true *p // panic: invalid memory address or nil pointer dereference pointers

Slide 65

Slide 65 text

coding time

Slide 66

Slide 66 text

type tree struct { v int l *tree r *tree } func (t *tree) Sum() int implement Sum 6 2 1 7 3 4 5 8 9

Slide 67

Slide 67 text

a first solution func (t *tree) Sum() int { sum := t.v if t.l != nil { sum += t.l.Sum() } if t.r != nil { sum += t.r.Sum() } return sum } 6 2 1 7 3 4 5 8 9

Slide 68

Slide 68 text

Code repetition: if v != nil { v.m() } Panic when t is nil var t *tree sum := t.Sum() // panic: invalid memory address or nil pointer dereference issues

Slide 69

Slide 69 text

type person struct {} func sayHi(p *person) { fmt.Println(“hi”) } func (p *person) sayHi() { fmt.Println(“hi”) } var p *person p.sayHi() // hi pointer receivers

Slide 70

Slide 70 text

nil receivers are useful 零

Slide 71

Slide 71 text

func (t *tree) Sum() int { if t == nil { return 0 } return t.v + t.l.Sum() + t.r.Sum() } nil receivers are useful: Sum

Slide 72

Slide 72 text

func (t *tree) String() string { if t == nil { return "" } return fmt.Sprint(t.l, t.v, t.r) } nil receivers are useful: String

Slide 73

Slide 73 text

func (t *tree) Find(v int) bool { if t == nil { return false } return t.v == v || t.l.Find(v) || t.r.Find(v) } nil receivers are useful: Find

Slide 74

Slide 74 text

keep nil useful if possible, if not NewX()

Slide 75

Slide 75 text

pointers slices maps channels functions interfaces how are these useful?

Slide 76

Slide 76 text

var s []slice len(s) // 0 cap(s) // 0 for range s // iterates zero times s[i] // panic: index out of range nil slices

Slide 77

Slide 77 text

var s []int for i := 0; i < 10; i++ { fmt.Printf("len: %2d cap: %2d\n", len(s), cap(s)) s = append(s, i) } append on nil slices

Slide 78

Slide 78 text

len: 0 cap: 0 [] len: 1 cap: 1 [0] len: 2 cap: 2 [0 1] len: 3 cap: 4 [0 1 2] len: 4 cap: 4 [0 1 2 3] len: 5 cap: 8 [0 1 2 3 4] len: 6 cap: 8 [0 1 2 3 4 5] len: 7 cap: 8 [0 1 2 3 4 5 6] len: 8 cap: 8 [0 1 2 3 4 5 6 7] len: 9 cap: 16 [0 1 2 3 4 5 6 7 8] → s is nil! → allocation → reallocation → reallocation → reallocation → reallocation $ go run slices.go

Slide 79

Slide 79 text

use nil slices they’re often fast enough

Slide 80

Slide 80 text

pointers slices maps channels functions interfaces how are these useful?

Slide 81

Slide 81 text

nil maps var m map[t]u len(m) // 0 for range m // iterates zero times v, ok := m[i] // zero(u), false m[i] = x // panic: assignment to entry in nil map

Slide 82

Slide 82 text

using maps func NewGet(url string, headers map[string]string) (*http.Request, error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, err } for k, v := range headers { req.Header.Set(k, v) } return req, nil }

Slide 83

Slide 83 text

NewGet( "http://google.com", map[string]string{ "USER_AGENT": "golang/gopher", }, ) using maps

Slide 84

Slide 84 text

GET / HTTP/1.1 Host: google.com User_agent: golang/gopher $ go run request.go

Slide 85

Slide 85 text

NewGet("http://google.com", map[string]string{}) using empty maps

Slide 86

Slide 86 text

GET / HTTP/1.1 Host: google.com $ go run request.go

Slide 87

Slide 87 text

NewGet("http://google.com", map[string]string{}) using empty maps

Slide 88

Slide 88 text

NewGet("http://google.com", nil) nil maps are valid empty maps

Slide 89

Slide 89 text

GET / HTTP/1.1 Host: google.com $ go run request.go

Slide 90

Slide 90 text

use nil maps as read-only empty maps

Slide 91

Slide 91 text

pointers slices maps channels functions interfaces how are these useful?

Slide 92

Slide 92 text

var c chan t <- c // blocks forever c <- x // blocks forever close(c) // panic: close of nil channel nil channels

Slide 93

Slide 93 text

coding time

Slide 94

Slide 94 text

func merge(out chan<- int, a, b <-chan int) a b out

Slide 95

Slide 95 text

func merge(out chan<- int, a, b <-chan int) { for { select { case v := <-a: out <- v case v := <-b: out <- v } } } a naive solution

Slide 96

Slide 96 text

2 2 1 2 2 1 2 1 2 1 2 1 2 1 1 2 2 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 … $ go run naive.go

Slide 97

Slide 97 text

var c chan t v, ok <- c // zero(t), false c <- x // panic: send on closed channel close(c) // panic: close of nil channel closed channels

Slide 98

Slide 98 text

func merge(out chan<- int, a, b <-chan int) { for { select { case v := <-a: out <- v case v := <-b: out <- v } } } a naive solution

Slide 99

Slide 99 text

checking for closed chans case v, ok := <-a: if !ok { aClosed = true continue } out <- v // analogous code for case b

Slide 100

Slide 100 text

checking for closed chans func merge(out chan<- int, a, b <-chan int) { var aClosed, bClosed bool for !aClosed || !bClosed { select { case v, ok := <-a: if !ok { aClosed = true; continue } out <- v case v, ok := <-b: if !ok { bClosed = true; continue } out <- v } } }

Slide 101

Slide 101 text

$ go run checkForClosed.go 1 1 2 1 1 2 2 1 1 2 1 2 2 1 2 1 2 1 2 2 fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan receive]: main.main() /Users/campoy/src/github.com/campoy/talks/nil/talk/code/chans. go:97 +0x15a exit status 2

Slide 102

Slide 102 text

and closing channel out func merge(out chan<- int, a, b <-chan int) { var aClosed, bClosed bool for !aClosed || !bClosed { select { case v, ok := <-a: if !ok { aClosed = true; continue } out <- v case v, ok := <-b: if !ok { bClosed = true; continue } out <- v } } close(out) }

Slide 103

Slide 103 text

and closing channel out func merge(out chan<- int, a, b <-chan int) { var aClosed, bClosed bool for !aClosed || !bClosed { select { case v, ok := <-a: if !ok { aClosed = true; continue } out <- v case v, ok := <-b: if !ok { bClosed = true; continue } out <- v } } close(out) }

Slide 104

Slide 104 text

1 1 2 1 1 2 2 1 1 2 1 2 2 1 2 1 2 1 2 2 $ go run closingOut.go

Slide 105

Slide 105 text

ship it! Fancy Gopher by Renee French

Slide 106

Slide 106 text

by hzeller on Flickr

Slide 107

Slide 107 text

let’s log case v, ok := <-a: if !ok { aClosed = true fmt.Println("a is now closed") continue } out <- v // analogous code for case b

Slide 108

Slide 108 text

$ go run withLogs.go 1 1 2 1 1 2 2 1 1 2 1 2 2 1 2 1 2 1 2 2 b is now closed … b is now closed 1 b is now closed … b is now closed 1 a is now closed

Slide 109

Slide 109 text

var aClosed, bClosed bool for !aClosed || !bClosed { select { case v, ok := <-a: if !ok { aClosed = true; continue } out <- v case v, ok := <-b: if !ok { bClosed = true; continue } out <- v } } close(out) let’s log

Slide 110

Slide 110 text

can we switch off a chan?

Slide 111

Slide 111 text

var c chan t <- c // blocks forever c <- x // blocks forever close(c) // panic: close of nil channel nil channels

Slide 112

Slide 112 text

switching off a channel case v, ok := <-a: if !ok { aClosed = true fmt.Println("a is now closed") continue } out <- v // analogous code for case b

Slide 113

Slide 113 text

case v, ok := <-a: if !ok { a = nil fmt.Println("a is now closed") continue } out <- v // analogous code for case b switching off a channel

Slide 114

Slide 114 text

func merge(out chan<- int, a, b <-chan int) { for a != nil || b != nil { select { case v, ok := <-a: if !ok { a = nil; continue } out <- v case v, ok := <-b: if !ok { b = nil; continue } out <- v } } close(out) } switching off a channel; no logs

Slide 115

Slide 115 text

1 1 2 1 1 2 2 1 1 2 1 2 2 1 2 1 2 1 2 2 $ go run closingOut.go

Slide 116

Slide 116 text

fancy party time! Fancy Gopher by Renee French

Slide 117

Slide 117 text

use nil chans to disable a select case

Slide 118

Slide 118 text

pointers slices maps channels functions interfaces how are these useful?

Slide 119

Slide 119 text

Go has first-class functions functions can be used as struct fields they need a zero value; logically it is nil nil funcs type Foo struct { f func() error }

Slide 120

Slide 120 text

lazy initialization of variables nil can also imply default behavior func NewServer(logger func(string, …interface{})) { if logger == nil { logger = log.Printf } logger("initializing %s", os.Getenv("hostname")) … } nil funcs for default values

Slide 121

Slide 121 text

pointers slices maps channels functions interfaces how are these useful?

Slide 122

Slide 122 text

The nil interface is used as a signal if err != nil { … } interfaces

Slide 123

Slide 123 text

why does nil *Person not equal nil interface? 零

Slide 124

Slide 124 text

summer type Summer interface { func Sum() int }

Slide 125

Slide 125 text

summer var t *tree var s Summer = t fmt.Println(t == nil, s.Sum()) // true 0

Slide 126

Slide 126 text

type ints []int func (i ints) Sum() int { s := 0 for _, v := range i { s += v } return s } summer

Slide 127

Slide 127 text

summer var i ints var s Summer = i fmt.Println(i == nil, s.Sum()) // true 0

Slide 128

Slide 128 text

nil values can satisfy interfaces 零

Slide 129

Slide 129 text

func doSum(s Summer) int { if s == nil { return 0 } return s.Sum() } nil values and default values

Slide 130

Slide 130 text

nil values and default values var t *tree doSum(t) // (*tree, nil) var i ints doSum(i) // (ints, nil) doSum(nil) // (nil, nil)

Slide 131

Slide 131 text

http.ListenAndServe("localhost:8080", nil) nil values and default values

Slide 132

Slide 132 text

use nil interfaces to signal default 零

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

nil is useful

Slide 135

Slide 135 text

pointers methods can be called on nil receivers slices perfectly valid zero values maps perfect as read-only values channels essential for some concurrency patterns functions needed for completeness interfaces the most used signal in Go (err != nil) nil is useful

Slide 136

Slide 136 text

nil is an important part Go 零

Slide 137

Slide 137 text

let’s not avoid nil

Slide 138

Slide 138 text

let’s embrace nil Thanks, @francesc golang.org/s/nil