How to postpone or defer an inner function call until the outer or surrounding function completes its execution in Go or Golang?
Tags: go
Published: 2022-08-26 15:14:11

---
  title: How to postpone or defer an inner function call until the outer or surrounding function completes its execution in Go or Golang?
  published: true
  tags: Go
  date: Fri Aug 26 2022 20:44:11 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/postpone-defer-inner-function-execution-until-outer-or-surrounding-function-completes-execution-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-46d7b70cd773f7c9d56eaca17e66c25d.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/postpone-defer-inner-function-execution-until-outer-or-surrounding-function-completes-execution-go-golang)
  
To postpone or defer an inner function's execution until the outer or surrounding function execution completes, you can use the `defer` keyword followed by the function call in Go or Golang.

### TL;DR

```go
package main

import "fmt"

// function that prints string to the terminal
func sayBye(){
    fmt.Println("Bye, Have a nice day!")
}

func main(){
    // postpone or defer the `sayBye()` function call until the
    // `main()` function execution completes
    defer sayBye()

    fmt.Println("I will be executed first")
}
```

For example, let's say we have a function called `sayBye()` which prints `Bye, Have a nice day!` to the user only after the `main` function execution completes.

To do that first let's make the `sayBye()` function that prints the string `Bye, Have a nice day!` to the console or terminal like this,

```go
package main

import "fmt"

// function that prints string to the terminal
func sayBye(){
    fmt.Println("Bye, Have a nice day!")
}
```

Now at the top of the `main()` function, we can use the `defer` keyword followed by the `sayBye()` function call like this,

```go
package main

import "fmt"

// function that prints string to the terminal
func sayBye(){
    fmt.Println("Bye, Have a nice day!")
}

func main(){
    // postpone or defer the `sayBye()` function call until the
    // `main()` function execution completes
    defer sayBye()
}
```

Since we don't have many lines of code to see the working correctly, let's add another `Println()` function to show the string of `I will be executed first` after the `defer` statement like this,

```go
package main

import "fmt"

// function that prints string to the terminal
func sayBye(){
    fmt.Println("Bye, Have a nice day!")
}

func main(){
    // postpone or defer the `sayBye()` function call until the
    // `main()` function execution completes
    defer sayBye()

    fmt.Println("I will be executed first")
}
```

The output of running the above code will look like this,

```bash
I will be executed first
Bye, Have a nice day!
```

As you can see that the `I will be executed first` string is shown before the string `Bye, Have a nice day!` even though it is evaluated first.

We have successfully postponed or deferred the function call. Yay 🥳.

See the above code live in [The Go Playground](https://go.dev/play/p/93y0XK87gTR).

That's all 😃!

### Feel free to share if you found this useful 😃.

---