# Understanding Defer, Panic and Recover

DevFeed: [Understanding Defer, Panic and Recover](<https://devfeed.tech/articles/understanding-defer-panic-and-recover-22053.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2013/06/understanding-defer-panic-and-recover.html>)

Published: 2013-06-08T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [ardan-labs](<https://devfeed.tech/tags/ardan-labs.md>), [blog](<https://devfeed.tech/tags/blog.md>), [code](<https://devfeed.tech/tags/code.md>), [defer](<https://devfeed.tech/tags/defer.md>), [error-interface](<https://devfeed.tech/tags/error-interface.md>), [function](<https://devfeed.tech/tags/function.md>), [go](<https://devfeed.tech/tags/go.md>), [go-programming](<https://devfeed.tech/tags/go-programming.md>), [golang](<https://devfeed.tech/tags/golang.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

This tutorial explains how Go's defer mechanism works, including how a deferred function runs when its containing function ends and can access captured state through a closure. It introduces defer, panic, and recover as the basis for try/catch-like protection in Go applications.

## Source excerpt

I am building my TraceLog package and it is really important that the package logs any internal exceptions and prevents panics from shutting down the application. The TraceLog package must never be responsible for shutting down an application. I also have internal go routines that must never terminate until the application is shut down gracefully. Understanding how to use Defer and Recover in your application can be a bit tricky at first, especially if you are used to using try/catch blocks. There is a pattern you can implement to provide that same type of try/catch protection in Go. Before I can show you this you need to learn how Defer, Panic and Recover work. First you need to understand the intricacies of the keyword defer. Start with this piece of code: package main import ( "fmt" ) func main() { test() } func mimicError(key string) error { return fmt.Errorf("Mimic Error : %s", key) } func test() { fmt.Println("Start Test") err := mimicError("1") defer func() { fmt.Println("Start Defer") if err != nil { fmt.Println("Defer Error:", err) } }() fmt.Println("End Test") } The MimicError function is a test function that will be used to simulate an error. It is following the Go convention of using the error type to return an indication if something went wrong. In Go the error type is defined as an interface: type error interface { Error() string } If you don't understand what a Go interface is at the moment then this might help for now. Any type that implements the Error() function implements this interface and can be used as a variable of this type. The MimicError function is using errors.New(string) to create an error type variable. The errors type can be found in the errors package. Test function produces the following output: Start Test End Test Start Defer Defer Error : Mimic Error : 1 When you study the output you see that the Test function started and ended. Then right before the Test function terminated for good, the inline defer function was called. Two inter