I need to use defer to free allocations manually created using C library, but I also need to os.Exit with non 0 status at some point. The
defer
C
os.Exit
For posterity, for me this was a more elegant solution:
func main() { retcode := 0 defer func() { os.Exit(retcode) }() defer defer1() defer defer2() [...] if err != nil { retcode = 1 return } }