Go for Beginners Part 4 - Error Handling in Go

Part 4 - Error Handling in Go

Hello!

This is a post from the Golang Series: Go Language for Beginners in 16 Parts!

In the today's post we're going to see how to handle errors in Go. Actually it's quite easy to do that and in this post we'll cover the basics of handling errors. In another post we'll deep dive error handling to see and resolve a few interesting challenges!

In Go, errors are treated as a normal value!

Let's create a function that calculates the square of a given number:

To use this code:

If you run the code the result will be:

An import validation is not allows negative numbers to be used. We should validate the value in the square function and return an error if the value is invalid:

Notice the function signature:

Now we're returning the type error to be checked by the caller function. So, it's time to create the entire code:

We're using a new package here: log. This is being used to log a Fatal error and exit the execution.

As you can see, the square function returns multiple results, being the error one of them. As I said before, an Error in Go is treat just as a normal value, so we just need to check if the value is nil:

If you're coming from Java, Ruby or C# for example, your code probably has a strong error handling, with different types of errors and inheritance, with errors checked by try..catch approach. Forget about that in Go : )

Ignoring Errors in Go

You can ignore a returned value from a function that has multiple values being returned. As an error in Go is just a value, you can ignore it also. To do that, just use the underscore:

If you run this code the output will be:

As you can see, the error has been ignored by the main() function.

That's all for today. In another post we'll know much more about Error handling in Go!

In the next post: Part 5 - Flow control in Go we're going to see Flow Control in Go!

Don't forget to see and fork the project on GitHub!

Follow us to keep up to date! \o/

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Go Language Tutorial for Beginners - Go Lang Characteristics - Hacking Code

Part 1 - Go Language Characteristics

This is the first Post of the complete series about Go Language!

Go Language Tutorial for Beginners - Declaring Variables in Go - Hacking Code

Part 2 - Declaring Variables in Go

How can we declare and use variables in Go? Check it out!

Go Language Tutorial for Beginners - Functions in Go - Hacking Code

Part 3 - Functions in Go

Do you want to try a more Functional Programming Style with Functions?

Go Language Tutorial for Beginners - Error Handling in Go - Hacking Code

Part 4 - Error Handling in Go

Errors always happen! How can we handle Errors in Go Language?

Go Language Tutorial for Beginners - Flow Control in Go - Hacking Code

Part 5 - Flow Control in Go

How to live without a Flow Control in Go? We can’t! Let’s see how to use it!

Go Language Tutorial for Beginners - Conditionals in Go - Hacking Code

Part 6 - Conditionals in Go

In this post we’re going to see how to work with Conditionals in Go!

Go Language Tutorial for Beginners - Switch in Go - Hacking Code

Part 7 - Switch in Go

Do you like to have many if statement in you code? So, let’s see switch statement in Go!

Go Language Tutorial for Beginners - Arrays in Go - Hacking Code

Part 8 - Arrays in Go

Data Structures in Go are really common! In this post we’re going to see Arrays in Go!

Go Language Tutorial for Beginners - Slices in Go - Hacking Code

Part 9 - Slices in Go

Slices in Go are really important and used in every code! Let’s learn about it!

Go Language Tutorial for Beginners - Maps in Go - Hacking Code

Part 10 - Maps in Go

What about using Maps in Go?

Go Language Tutorial for Beginners - Custom Types in Go - Hacking Code

Part 11 - Custom Types in Go

Let’s learn how to create and user Custom Types in Go

Go Language Tutorial for Beginners - Structs in Go - Hacking Code

Part 12 - Structs in Go

Structs in Go allow us to have types of an abstraction together! Let’s see how!

Go Language Tutorial for Beginners - Methods in Go - Hacking Code

Part 13 - Methods in Go

Methods in Go are created a little bit different from other Languages. Let’s see how?

Go Language Tutorial for Beginners - Interfaces in Go - Hacking Code

Part 14 - Interfaces in Go

Interfaces in Go are really different but exciting feature! Let’s see why!

Go Language Tutorial for Beginners - Concurrency with Goroutines in Go - Hacking Code

Part 15 - Concurrency with Goroutines in Go

Concurrency in Go is fantastic and simple! We’re going to see Goroutines now!

Go Language Tutorial for Beginners - Concurrency with Channels in Go - Hacking Code

Part 16 - Concurrency with Channels in Go

We can’t live with Goroutines without Channels when working with Concurrency in Go! Let’s see why?

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: