Introduction
In episode 4, Miki defined an enumerated type that satisfied Go’s fmt.Stringer
interface. By implementing the fmt.Stringer
interface, Miki can specify how his enumerators were printed within a formatted string and in this case, he expected the values to be displayed as a predetermined text value. Miki also pointed out how using the value of the method’s receiver within the Stringer
method can result in a recursive loop.
In this video, Miki will define an unusual function that returns the number of lines in a file with the io.Writer
interface. The first step he’ll take is to declare a variable and set it to the value returned by the os
package’s Open
function. Miki does this because the concrete type of the variable returned satisfies the io.Reader
interface. The next step he takes is to define a type that is an alias of a primitive type and implement the methods required for the type alias to satisfy the io.Writer
interface. Watch and learn how to implement the io.Writer
interface and how it interacts with Go’s io
package.
Things you will learn in this video
- Implement the
io.Writer
interface with a type that’s an alias of a primitive type. - Pass a variable as an interface type to function.
- Repurpose the
io.Writer
interface to count lines of code.
Video