1. Home
  2. Training Library
  3. DevOps
  4. Courses
  5. Introduction to the Go Programming Language

Panic/Recover

play-arrow
Start course
Overview
DifficultyBeginner
Duration2h 17m
Students492
Ratings
4.7/5
starstarstarstarstar-half

Description

If you're thinking about engineering the next big dotcom application then you should seriously consider using Go!! 

The Go Programming Language is without doubt one of the hottest languages to learn, particularly in this cloud native era. More and more companies are adopting Go to engineer highly performant, stable and maintainable applications. Popular projects such as Docker, Kubernetes, Terraform, Etcd, Istio, InfluxDB have all been built successfully using Go!! 

This introductory level training course is designed to bring you quickly up to speed with the many key features that the Go programming language provides. You'll also learn how to setup your own Go development environment - consisting of the Go toolchain, Visual Studio Code, and several related Go based extensions - all to ensure that you are able to be productive writing your own source code.

We’d love to get your feedback on this course, so please give it a rating when you’re finished. If you have any queries or suggestions, please contact us at support@cloudacademy.com.

Learning Objectives

By completing this course, you will:

  • Learn about what makes Go a great language
  • Learn how to install the Go toolchain
  • Learn how to setup Visual Studio Code to edit and debug Go programs
  • Learn how to work with the Go Playground to test and run snippets of Go code
  • Learn and understand the basic Go language syntax and features
  • Learn how to use the Go tool chain commands to compile, test, and manage Go code
  • And finally, you’ll learn how to work with and manage Go modules for module dependency management

Intended Audience

This course is intended for:

  • Anyone interested in learning the Go Programming Language
  • Software Developers interested in using Go to compile and test Go based applications
  • DevOps practitioners looking to learn about Go to support Go based applications

Prerequisites

To get the most from this course, you should have at least:

  • A basic understanding of software development and the software development life cycle

Source Code

All sample Go source code as used and demonstrated within this course can be found here:

Transcript

- [Jeremy Cook] Go provides additional capabilities when exceptions occur in the form of panics and recovery. Custom panics can be thrown when something exceptional and unexpected has happened. Panics, if not recovered from, will cause the program's execution to be exited with a non-zero exit code, which the operating system uses to determine that something abnormal has just happened. A panic can be recovered from if and only if, the surrounding function in which the panic happened has a deferred function that itself then calls the inbuilt recover function and then does something appropriately to resume. 

In the example provided here, the system function will throw a runtime panic on Line 16. Since we're trying to access the data slice, we know it's still empty. The anonymously declared defer function on Lines 8 to 13 will now take program execution from the panic, and since it calls the recover function within itself, it enables the panic to be recovered from. Interestingly, because the system function return statement didn't get hurt, the returned value is received back within the main function on Line 25 will be zero and not one. Zero being the zero value for the data type, int, which is the date type for the returned value. 

Finally, on Line 28 for demo purposes only, I manually invoked panic with the message, die. And since there is no recovery option available, the program execution exits with a non-zero exit code. Let's now run this example and examine the resulting output. Here, we can see the following messages are printed out: system started, recovered, blah, and zero. Before, the program then finally halts due to the panic, die, exiting with the status code of 2. 

Therefore, in summary, you've just observed how to handle and recover from runtime panics, using the inbuilt recover function, how to create and raise panics, and how an unhandled panic will cause program termination with a non-zero exit code.

About the Author

Students27937
Labs32
Courses93
Learning paths22

Jeremy is the DevOps Content Lead at Cloud Academy where he specializes in developing technical training documentation for DevOps.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 20+ years. In recent times, Jeremy has been focused on DevOps, Cloud, Security, and Machine Learning.

Jeremy holds professional certifications for both the AWS and GCP cloud platforms.
 

Covered Topics