GOLANG

Defer, Panic and Recover in Go

As we have seen the earlier tutorial, Go treats error as a value. But at times, Go breaks the program in runtime and such types of errors are called as Runtime Panics. A panic can be triggered manually and we can recover from this panic using built-in capabilities provided by Go.

Uday Hiwarale
Jun 17, 2019 · 8 min read
Image for post
Image for post
(source: unsplash.com)
Image for post
Image for post
https://play.golang.org/p/lM9hggLnCM2
Image for post
Image for post
https://play.golang.org/p/Q7R8cU1OYuj
panic(interface{})
Image for post
Image for post
https://play.golang.org/p/kDcZ4-CkC8A
Image for post
Image for post
https://play.golang.org/p/uSXAmj61ADt

Recovering from the panic

recover() interface{}
Image for post
Image for post
https://play.golang.org/p/lnOiERNgRwX
Image for post
Image for post
https://play.golang.org/p/8du5HAiSRta
Image for post
Image for post
https://play.golang.org/p/zAlYEyHp5Kp
Image for post
Image for post
https://play.golang.org/p/NGjWyrSHcZj

Handling function return values

Image for post
Image for post
https://play.golang.org/p/B_HGZOTi2nJ
Image for post
Image for post
https://play.golang.org/p/sc8b82gysaJ
Image for post
Image for post

RunGo

A go-to guide for learning Go programming language

Uday Hiwarale

Written by

Senior Software Engineer / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Uday Hiwarale

Written by

Senior Software Engineer / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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