Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

GopherCon Israel

February 11, 2019
Tweet

More Decks by GopherCon Israel

Other Decks in Programming

Transcript

  1. What it’s all about “defers the execution of a function

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

    until the surrounding function returns.” Crosspond to the finally Java or Python.
  3. 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”
  4. 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?!
  5. 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”.
  6. 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.
  7. Defer in a infinite loop - risks Problems Memory leak

    Too many files descriptors. Defer will never happen
  8. 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
  9. Links Credits to Inanc Gumus 1. 5 Gotchas of Defer

    in Go   —   Part I 2. Go Defer Simplified with Practical Visuals