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
Post a Comment