...

/

The defer Pattern

The defer Pattern

This lesson briefly discusses the uses of the defer pattern in Go language.

Using defer ensures that all resources are properly closed or given back to the pool when the resources are not needed anymore. Secondly, it is paramount in Go to recover from panicking.

Closing a file stream

// open a file f
defer f.Close()

Unlocking a locked resource

...