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

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Custom errors

Custom errors

- As we have seen errors and go are implemented via the error interface. This means we can create our own custom error implementations. Custom errors allow us to manage workflow and provide detailed information about an error beyond the scope of the error interface. Here we have a couple of types being defined. The first, model, is based on a map with a string key and in any value for its type. So for example, age is 27, name Jimmy. Next, a store struck is defined with a data field. This data field is a map of table names to their models. The data fields types a map with a string key and a slice of models. The store has a method all that takes a string that will return all the models in the store for the given table. If the table doesn't exist, an error will be returned. This error is created using the funct error F function. Finally, on the right is a test that asserts if a given table doesn't exist in the store. This test, however, is lacking. Yes, it asserts that an error was…

Contents