Introduction
In episode 2, Miki examined the impact interfaces have on the performance of a Go program. To perform this experiment, Miki invoked a type’s method in two ways: with the concrete type and as an interface function to measure the difference in execution time. The conclusion of this experiment was that calling a method with the concrete type is faster than using an interface. During the experiment, Miki made use of the build flag -gcflag=’-m’
to display which variables were being allocated on the heap and where the compiler was automatically inlining
function calls.
In this video, Miki will take a look at the error interface and how to define a type to satisfy it. The first step Miki takes is to implement a custom type with a method named Error
that returns a string
. Once the interface is satisfied, Miki will write a test that will invoke a function that returns his custom error type and verify if the returned value does not equal to nil, if so, the test fails. The odd thing here is that the test fails although Miki does not set a value for the returned error. Watch and learn how to properly define a type that implements Go’s error
interface.
Things you will learn in this video
- Implement Go’s
error
interface. - How does Go determine if an
error
value is nil. - How to avoid false-positives with custom implementations of
error
interface.
Video