Return statements. Language Specification

A "return" statement in a function F terminates the execution of F, and optionally provides one or more result values. Any functions deferred by F are executed before F returns to its caller.

function F, arbitrary name for a function? In other words, the following is †rue for any function, for example, a function that we will call F
past tense: deferred ; past participle: deferred

  1. put off (an action or event) to a later time; postpone.
    What do they mean deferred?
    Returned to its caller?

The are refering to the defer keyword, which moves a function call to after the function is leaved.

f, _ := File.Open("foo.txt")
defer f.Close()

// read and process the file

When somewhere causes a panic, or there is an early return or simply the function is left because there is nothing more to do, then the defered call is done and the file closed.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.