Join David Gassner for an in-depth discussion in this video Handling errors, part of Learning Go.
- I've previously shown how functions…can return errors in Go, unlike most…C-style languages, Go doesn't support…classic exception-handling syntax,…with the usual try and catch keywords,…and since there isn't any type inheritance,…the concept of a super-type such as exception…and subtypes for different kinds of exceptions…doesn't fit in this language either.…Instead, an error in Go is an instance…of an interface that defines a single method,…named error, and that method returns a string…and that string is the error message,…so I'm going to start off here with this bit of code.…
I'm calling a method named Open from the OS package,…and I'll add the import statement for the package up here,…and then, after I've called the method,…I'll add some conditional logic.…I'll ask whether the error is nil,…with if err matches nil,…and if it is,…that means that the operation succeeded,…and I can print something out.…Otherwise, I'll print out the content of the error message.…
Now, you can do it simply like this,…by passing the error object into the Println function,…
- Installing Go tools
- Creating and compiling a Go workspace
- Exploring variables, constants, and types
- Storing ordered and unordered values
- Grouping related values in structs
- Programming conditional logic and loops
- Defining and calling functions
- Handling errors
- Working with files
- Creating a simple HTTP server
Skill Level Beginner
1. Getting Started
2. Essential Go Skills
3. Managing Simple Values
4. Managing Complex Types and Collections
5. Managing Program Flow
6. Structuring Go Code
7. Working with Files and the Web
Next steps2m 26s
- 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.