Introduction
In episode 3, Miki implemented a type that satisfied Go’s error
interface. The odd thing about his type was it would be considered not-nil although no value was set for it. To get a better understanding of the situation, Miki gives a brief explanation of how Go determines if an error value is nil, and in this case, Miki specified the type of the variable returned to be a pointer of his custom error type which blindsided Go’s mechanism to determine a nil value. Miki chose this example because it demonstrated the importance of declaring an error with its type set as the error
interface.
In this video, Miki will focus on Go’s fmt.Stringer
interface and how it can be used to control how Go prints your type. The first step he’ll take is to define an enumerated type representing different keys loosely based on the book Ready Player One. Miki will then implement the method required for Go’s fmt.Stringer
interface which will specify how his enumerated type will be printed as a string. Watch and learn how to define a function that will decide how your type is formatted and printed.
Things you will learn in this video
- Define a Go enum with a user-defined type.
- How to implement Go’s
fmt.Stringer
interface. - Specify how your type is represented with string formatting.
Video