RosettaCodeData/Task/Flow-control-structures/Go/flow-control-structures-2.go
2023-07-01 13:44:08 -04:00

18 lines
458 B
Go

import "os"
func processFile() {
f, err := os.Open("file")
if err != nil {
// (probably do something with the error)
return // no need to close file, it didn't open
}
defer f.Close() // file is open. no matter what, close it on return
var lucky bool
// some processing
if (lucky) {
// f.Close() will get called here
return
}
// more processing
// f.Close() will get called here too
}