Lesson 17Handling Errors

Programming code, regardless of the language, can include a variety of errors, including syntax errors, logic, division by zero, and missing files, to name just a few. As a result, every programming language has built-in mechanisms to deal with errors. In this lesson, we will look at error handling in Go.

ERRORS IN GO PROGRAMS

It is important to point out that there are several types of errors. There are syntax errors that are generally typos made when a developer is writing the code. These can be simply a misspelled word or a coding statement that is missing a character. Syntax errors generally stop a program from compiling and running, so a programmer must fix them before the program will run. Like most languages, the Go compiler will indicate syntax errors when the program is compiled or run. Examples of common syntax errors include:

  • Using the wrong capitalization such as using println instead of Println
  • Failing to declare a variable before using it
  • Trying to assign a value of one type to a variable of a different type

There are also logic errors. With logic errors, the code will generally compile and run but the output might not be what is expected. Like syntax errors, logic errors are generally caused ...

Get Job Ready Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.