Today I Learned

2025/02/12

go

That defer statements are block-scoped:

package main

import "fmt"

func main() {
	defer fmt.Println("end func") // runs at end of function
	fmt.Println("start func")
	{
		defer fmt.Println("end block") // runs at end of block
		fmt.Println("start block")
	}
}
// Output:
// start func
// statt block
// end block
// end func

see https://go.dev/play/p/2OYL2-g1iUE