In case of a non-nil error Close will not be executed and that is exactly what you want. The power of defer becomes obvious with more code after the defer which does not have to deal with any cleanup of previous steps because of the defer.
Am Fr., 4. Jan. 2019 um 07:09 Uhr schrieb 伊藤和也 <kazya.ito.dr...@gmail.com>: > In the first example, "ok" was not printed. > > func main() { > if(true) { > return > } > defer fmt.Println("ok") > } > > So in the second example, is the Close function executed after an error > occurs and returned. > > func main() { > src, err := os.Open("text.txt") > if err != nil { > return > } > defer src.Close() > } > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.