Syntax

Go Defer a Call Syntax

In Go, defer a call: runs a call when the function returns — defer f.Close()

Quick Answer

Syntax
defer f.Close()
GoSyntax

Defer a Call

Code example
defer f.Close()

Runs a call when the function returns.

Learn More

How to defer a call in Go

  1. Open your Go file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Go Defer a Call Syntax

Use defer f.Close().

Related Syntax References

Frequently asked questions

What is the Go syntax for defer a call?

Use this pattern: defer f.Close()

Can I copy this Go code example?

Yes. Use Copy code and adapt the example to your program.

Is this Go syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify