39. Go - Defer

Defer is used to delay function execution to initialize after the surrounding statements/functions below are returned.

Since defer will always get executed when the surrounding function returns, it is a good place to attach cleanup code such as:

Closing open files
Releasing network resources
Closing the Go channel
Committing database transactions
And so on

Example:
package main

import "fmt"

func main() {
    defer fmt.Println("Hello Word")
    fmt.Println("Introduction to defer statement in Go")
}


Comments

Popular posts from this blog

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

20. Data Analytics - Analyze Data to Answer Questions - Week 1

3. Algorithms - Selection Sort