Skip to content

Latest commit

 

History

History
29 lines (19 loc) · 825 Bytes

golang-defer.md

File metadata and controls

29 lines (19 loc) · 825 Bytes

Title: Golang: Deferring statements Tags: golang

The defer keyword allows you to defer a statement executement until just before the function ends.

This will print the first print statement last and the last print statement first.

	defer fmt.Println("Last")
	fmt.Println("First")

You can also defer anonymous functions.

	defer func() {
		// Do stuff here
	}()

Deferring most useful for the closing of files.

	aFile, err := os.Open("filename")
	if err !=nil {
		log.Fatal("Can't open file")	
	}
	defer aFile.Close()
	// More statements

In the above, you have ensure the file is closed by the time the function or method ends with the defer aFile.Close() statement.

If you exit with os.Exit() or log.Fatal, for example, the defer call will not be called, but the runtime will close any open files for you.