Junior — Middle
Millal Go koodi täitmise protsessis kutsutakse defer operaator?
sobes.tech AI
Vastus AI-lt
The defer statement in Go postpones the execution of the specified function until the surrounding function exits. In other words, the call to the function passed to defer occurs just before the current function returns. This is useful for releasing resources, closing files, unlocking mutexes, etc.
Example:
func example() {
defer fmt.Println("Deferred call")
fmt.Println("Main code")
}
// Output will be:
// Main code
// Deferred call