Defer in V

Posted by yhuang
Public (Editable by Users)
V
None
Edit
// A defer statement defers the execution of a block of statements until the end of scope or until surrounding function returns.
fn read_log() {
	f := os.open('log.txt')
	defer { f.close() }
	// ...
	if !ok {
		// defer statement will be called here, the file will be closed
		return
	}
	// ...
	// defer statement will be called here, the file will be closed
}