package main
import (
"fmt"
)
func main() {
fmt.Println("Start")
defer fmt.Println("Middle")
fmt.Println("End")
}
package main
: Defines that the current file belongs to the main
package.import ("fmt")
: Imports the fmt
package, which provides functions for formatted I/O.func main() { ... }
: Defines the main
function, which serves as the entry point of the program.fmt.Println("Start")
: Prints "Start" to the standard output.defer fmt.Println("Middle")
: Defers the execution of fmt.Println("Middle")
until the surrounding function (main
in this case) finishes its execution.fmt.Println("End")
: Prints "End" to the standard output.- When
main
finishes execution, the deferred function fmt.Println("Middle")
executes, printing "Middle" after "End".