Overview

Welcome, aspiring programmer! Today, we're learning about error handling in Go. This subject is critical for addressing potential issues in your programs. Through practical examples, we'll see how Go's approach to error management contributes to the design of resilient and robust software.

Errors are inevitable in any program. However, by properly handling errors in Go, we can create solid applications that perform reliably even when things don't go as planned.

Understanding the Need for Error Handling

Life often throws us curve balls, and the same is true in the world of programming. Unexpected situations can arise — such as a user providing the wrong type of input or a required file being unlocatable. Anticipating and dealing with these scenarios is what error handling is all about.

Working with Error Handling in Go

Unlike other languages, Go doesn't implement traditional try/catch blocks. Instead, Go encourages explicit error checking by treating errors as ordinary variables that can be declared and manipulated.

Below is an example of how we might handle an error in Go:

Consider a scenario where you attempt to open a non-existent file. We open the file with os.Open. To use it, you need to import "os".

In this case, Go essentially says, "Okay, I'll TRY to open this file. Uh oh, it doesn't exist." In response, it generates an error that you can handle proactively.

Implementing Error Handling in Go

Let's write two Go scripts to demonstrate error handling. Each script will provide unique messages for successful and unsuccessful executions.

First, let's look at a block of code running without errors:

Secondly, we simulate a scenario that generates an error:

Note that the code can keep being executed if the error is handled.

Handling Non-existent Keys in Maps

In Go, when accessing a value from a map using a key, you can also determine if the key exists in the map. This is achieved with a syntax that returns two values: the value associated with the key and a boolean indicating the presence of the key.

Here's a concise example:

  • value is the value for key.
  • ok is true if key exists, false otherwise.

Let's see it in action:

If "Diana" isn't a key in heroes, ok will be false, and we print "Hero not found." This method provides a simple yet effective way to handle missing keys in maps, ensuring your code can gracefully deal with unexpected situations.

Handling Common Go Errors

Internally, Go has a variety of built-in errors such as ErrInvalidArgument, ErrPermission, ErrExist, and ErrNotExist. Each can be uniquely interpreted to better understand your programs' faults. Let's see how to handle multiple errors:

In this example, we handle specific errors related to opening a file. These include cases where the file doesn't exist or when there are permission issues. Any other unforeseen error will trigger the default case.

Lesson Summary

Congratulations! You've learned how to handle errors in Go effectively. This is a crucial skill for creating resilient Go applications.

Up next are a series of practice exercises designed to help polish and solidify your understanding of this concept. Remember, mastering error handling requires practice, so dedicate your efforts to these exercises!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal