atc0005 / learn

Various topics that I want to learn more about, either for professional development or for my own use
0 stars 0 forks source link

Custom error types, nil interfaces #33

Open atc0005 opened 5 years ago

atc0005 commented 5 years ago

From the Learn Go in 3 Hours, Chapter 5, Errors video:

While using your own error type is a good practice, you need to be careful. The Go error handling pattern always compares the return error value against nil. In order to understand how to properly return a custom error from a function requires you to understand how nil works with interfaces. The zero value for all interfaces is nil, but this nil is a bit different from the nil for pointers, slices, or maps. An interface is considered nil only if it isn't associated with any underlying value, even an underlying nil value.

I'm going to need to review this further as it isn't "clicking" yet for me.

refs: https://www.packtpub.com/application-development/learn-go-3-hours-video

atc0005 commented 5 years ago

Later in the video:

If you are defining your own error types you need to be sure that you never define a variable to be your own error type, because if you return that variable it will not appear nil even if no error actually occurred in your function.

This is good guidance.