All you need to know about defer & closure in Go-Lang
Published: 2023-04-12 08:41:42

---
title: All you need to know about defer & closure in Go-Lang
published: true
date: 2023-04-03 06:30:39 UTC
tags: 
canonical_url: https://toothsy.hashnode.dev/all-you-need-to-know-about-defer-closure-in-go-lang
---

![cant really begin the article without the mascot](https://cdn.hashnode.com/res/hashnode/image/upload/v1680093183473/e9dd416f-d225-4a7e-8826-e98f08f0e832.jpeg)

Hello, fellow internet user, before we begin, this article assumes you have the following pre-requisites

- You know about functions in go

- have an understanding of the local and global scope

- Have your vsc setup for go-lang, because itll be a delightful experience with autocomplete, linting and auto-imports

With that out of the way, onto the main issue at hand, young padawan.

### Just what is defer all about?

Just so we're on the same page, you **always** use `defer` keyword on a function call. Now what defer means is that the execution of the deferred function is delayed till the end of the surrounding function.

- ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680094897853/28cb9870-801c-4291-a163-1613737f2780.png)

- One thing to note is that if you have multiple `defer` keywords in a single function, they will be executed in First In Out Last, or `FILO` order, in simple words, the reverse order in which they were deferred.

- Let's go a little crazy for a second and add `defer` keyword to everything you see, try predicting the output.

- ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680095330753/0602f764-c58a-478a-b9f6-00585db5287d.png)

- Things to note, the **deferred function belongs to the surrounding function** and **not the block scope,** so if you deferred a function in an `if` block, or a `for` loop, the deferred function will always execute after the surrounding function ends, not before the loop or if statement.

### Okay, but what if I wanted to return something from a deferred function?

- well, you can't do it, your function needs to be the kind that returns nothing, or you can have a return and choose to defer it but you'll have to ignore the return value.

- But go has this feature called [named return](https://go.dev/tour/basics/7), which makes things a little interesting.

- ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680097533269/b68bbcb3-d065-41d3-b724-a5a3d8570313.png)

## Okay now for closures

- Closures are function values that reference variables residing outside of the function in question.

- This piece of code is shamelessly stolen from the go [docs](https://go.dev/tour/moretypes/25) because it is actually that effective to understand.

## let's mix things up!!

- it gets more interesting when you defer an anonymous function in a loop, as the function loses its meaning outside the scope of the loop, the compiler has to keep track of the deferred function calls in different ways, have a look below.

- ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680186400557/65fe35c0-b005-40b2-995a-98bf53f0c7de.png)

- but in the other loop, in line 17, we store the **value** that `index` holds along with the function, as dictated by the [docs here](https://go.dev/ref/spec#Defer_statements),

> Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked.

Hope you learned something from this article, if there are any incorrect assumptions made on my behalf, please do tell :), nothing would delight me more than dispelling my inaccurate understandings.

Meanwhile, I'll just link the things that helped me write this article

- [another cool article on defer](https://blog.learngoprogramming.com/gotchas-of-defer-in-go-1-8d070894cb01)

- [This Y combinator discussion was quite insightful](https://news.ycombinator.com/item?id=14668955)

- I may or may not have taken things from this [stack-overflow question](https://stackoverflow.com/questions/16010694/how-golangs-defer-capture-closures-parameter)

Thanks for reading and hope you have a great day 😁