Slide 1

Slide 1 text

Go Defer Eldad Marciano Software Engineer @Red Hat

Slide 2

Slide 2 text

Agenda ● Defer Scope ● Defer Order ● Using defer with direct vs pointers

Slide 3

Slide 3 text

What it’s all about “defers the execution of a function until the surrounding function returns.”

Slide 4

Slide 4 text

What it’s all about “Defers the execution of a function until the surrounding function returns.” Crosspond to the finally Java or Python.

Slide 5

Slide 5 text

What it’s all about “defers the execution of a function until the surrounding function returns.” Crosspond to the finally Java or Python. “finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling”

Slide 6

Slide 6 text

Python finally scope

Slide 7

Slide 7 text

Go defer in function scope

Slide 8

Slide 8 text

What is all about “defers the execution of a function until the surrounding function returns.” Crosspond to the finally Java or Python. “finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling” The tricky part?!

Slide 9

Slide 9 text

Thumb rules Defer belongs to func and not to a block.

Slide 10

Slide 10 text

Thumb rules Defer belongs to func and not to a block.

Slide 11

Slide 11 text

Thumb rules Defer belongs to func and not to a block.

Slide 12

Slide 12 text

Thumb rules Defer belongs to func and not to a block. Remember that Defer stacked the defer calls. “When a function returns, its deferred calls are executed in LIFO”.

Slide 13

Slide 13 text

Defer Stack LIFO

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Thumb rules Defer belongs to func and not to a block. Remember that Defer stacked the defer calls. “When a function returns, its deferred calls are executed in LIFO”. Be Careful when use defer inside infinite loop.

Slide 16

Slide 16 text

Defer in a infinite loop

Slide 17

Slide 17 text

Use infinite loop with pre-defined func

Slide 18

Slide 18 text

Use infinite loop with pre-defined func

Slide 19

Slide 19 text

Use infinite loop with pre-defined func

Slide 20

Slide 20 text

Use infinite loop with pre-defined func

Slide 21

Slide 21 text

Defer in a infinite loop - risks Problems Memory leak Too many files descriptors. Defer will never happen

Slide 22

Slide 22 text

Thumb rules Defer belongs to func and not to a block. Remember that Defer stacked the defer calls. “When a function returns, its deferred calls are executed in LIFO”. Be Careful when use defer inside infinite loop. Defer order, be Careful with sleeps or periodical functions

Slide 23

Slide 23 text

Python finally

Slide 24

Slide 24 text

Go Defer

Slide 25

Slide 25 text

Defer gotchas

Slide 26

Slide 26 text

Links Credits to Inanc Gumus 1. 5 Gotchas of Defer in Go   —   Part I 2. Go Defer Simplified with Practical Visuals

Slide 27

Slide 27 text

Q & A