From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Errors

Errors

- A lot of languages use the concept of exceptions. 2 When something goes wrong, an exception will be thrown. 3 This exception then needs to be caught. 4 When catching an exception, you have the opportunity 5 to log the exception 6 and possibly move on with an alternative code path. 7 Or re-raise the exception 8 and let the developer upstream deal with the problem. 9 Go takes a different approach and treats errors as values. 10 These values are returned and managed 11 instead of throwing and capturing exceptions. 12 In Go, errors are represented by the error interface. 13 Go provides two quick ways 14 to implement the error interface in our code. 15 The first is through the errors package New function. 16 This function takes a string 17 and returns an implementation of error 18 that uses a supplied string as the message. 19 So here we are creating a new error 20 with the errors.New function 21 and passing it the value…

Contents