go defer / Eldad Marciano

go defer / Eldad Marciano

As you may know golang is a GC lang. many times we write a peace of code, but we may forgot to defer some objects. memory leaks, over allocations, guess what we may defer some objects. in this talk i’m going dive into some edge cases that might cause to bad performance and even memory leaks

Many times we are not deferring an resources while coding applications. this is very important to use modules that has good defering, also it’s tricky where we’re going to place the defer statement.
Lets drill down into few examples, bad and good principals. stay tuned

C1508d6ff2ed86b2b4fedef9a3fe558f?s=128

GopherCon Israel

February 11, 2019
Tweet

Transcript

  1. Go Defer Eldad Marciano Software Engineer @Red Hat

  2. Agenda • Defer Scope • Defer Order • Using defer

    with direct vs pointers
  3. What it’s all about “defers the execution of a function

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

    until the surrounding function returns.” Crosspond to the finally Java or Python.
  5. 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”
  6. Python finally scope

  7. Go defer in function scope

  8. 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?!
  9. Thumb rules Defer belongs to func and not to a

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

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

    block.
  12. 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”.
  13. Defer Stack LIFO

  14. None
  15. 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.
  16. Defer in a infinite loop

  17. Use infinite loop with pre-defined func

  18. Use infinite loop with pre-defined func

  19. Use infinite loop with pre-defined func

  20. Use infinite loop with pre-defined func

  21. Defer in a infinite loop - risks Problems Memory leak

    Too many files descriptors. Defer will never happen
  22. 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
  23. Python finally

  24. Go Defer

  25. Defer gotchas

  26. Links Credits to Inanc Gumus 1. 5 Gotchas of Defer

    in Go   —   Part I 2. Go Defer Simplified with Practical Visuals
  27. Q & A