Don't Panic! Go has you covered

James Perkins

Posted on March 31, 2020


In this short blog post we are going to cover Panic in Go. Go has a fantastic functionality that lets you panic if you get an unexpected error and want you application to stop the execution.

A common use of panic is to abort if a function returns an error that we don’t know how to handle. Here’s an example of panic if we get an unexpected error when creating a new file.

package main import "os" func main() { panic("an unexpected error has happened! I am Panicking") _, err := os.Create("/testing/file") if err != nil { panic(err) } }

If you were to run this you would get a the following printed to your console panic: an unexpected error has happened! I am Panicking and any goroutine traces are printed, and the application will exit with a non-zero status in this case a 2 status, you can try for yourself by using this link to the go playground.


More Posts

Cover Image for Udacity React Nanodegree review - Part 1

Udacity React Nanodegree review - Part 1

I finished the first part of the nanodegree a few days ago and decided to give an indepth review of what is covered, what you learn and what is completed by the end of it.

Cover Image for Why you don't need to be a mathematics genius to be a developer.

Why you don't need to be a mathematics genius to be a developer.

When you first start thinking about becoming a developer, one of the things you might hear is you need to be a math(s) wizz. I am here to tell you that a small number of developers need the mathematics outside of algebra.