Learn the Go way of returning errors in multiple values from a function in this video. See the error type and how it's used throughout Go to signal errors and learn how to create custom error values and return them.
- [Instructor] Go functions can return more than one value. This is used extensively in Go to signal errors. The function that can error will usually return the error value as the last value returned. Here we have an s, q, r, t function, which calculates the square root of a number. But, unlike the one in the math standard library this one will return error on negative numbers. As we can see, we return two values. One is float 64, which is the result, and the other one is of type error.
Error is a built-in type that's used throughout Go. First we check. If n is smaller than zero, we return zero point zero because we have to return some value for the float, but also use the fmt dot error f function to create a new error. We'll reach line 13 if n is not negative, and then we return the square root of n and nil. Nil is the value Go uses to signal nothing. It is very much like null or none in other languages.
Now, let's use our function. First with a positive number. So, s one and err equal the square root of two point zero, and then we check. In line 18, if the err is not nil, it means some error happened. We're going to print out the error otherwise, we're going to print out the result. You are going to write a lot of if err does not equal nil code. You might find this tedious, but I personally found it made my code much more robust, and forced me to think about every error.
Now we try with the negative number in line 24. Because square root minus two point zero and then if the err is not nil, we print the error, otherwise, we print the result. Let's save it and run. And we see that in the first case, with the positive number, we printed the result. And with the negative number, we printed the error which was formatted nicely with the percent s verb. Go also has something called panic, which is somewhat similar to exceptions in other languages.
The use of panic is discouraged in Go and is considered an anti-pattern. The way to signal an error is to return it.
- Go basic types, conditionals, and loops
- Go functions
- Object-oriented programming with Go
- Error handling
- Concurrent programming with channels
- Testing and project management
- Networking with JSON and HTTP
Skill Level Beginner
Programming Foundations: Fuzzy Logicwith Erin Colvin41m 50s Intermediate
Getting started1m 34s
1. Getting Started
2. Go Basics
5. Error Handling
7. Project Management
Next steps1m 12s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.