Golang in sixty seconds — panic

Photo by Jasmin Sessler on Unsplash

We can use the panic function to cause a runtime error in Golang. This can be useful when paired with the recover function for error handling within our application. We may be tempted to use recover like this:

panic("AAAH!")
str := recover()
fmt.Println(str)

However we will never reach the line after panic as it will terminate the program. We therefore need to make use of the defer

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Richard Bell

Richard Bell

Father to a beautiful baby girl | Husband to a generous wife | Lead Software Engineer | Hobby collector | Support me at ko-fi.com/richardtbell