Error handling

Deferred changes to named results

Return expressions assign named result variables before deferred functions execute. A deferred closure can modify those variables before the caller receives them. Use this deliberately rather than hiding ordinary return logic. Example output: '5'.

Formal Syntax & Structure

go syntaxgo
func f() (n int) { defer func(){n++}(); return 4 }

Working Example

Example Snippetgo
package main
import "fmt"
func f() (n int) { defer func() { n++ }(); return 4 }func main() {  fmt.Println(f())}
Go Reference