1

Interfaces 101 : Determine LOC with io.Writer Ep. 5

 1 year ago
source link: https://www.ardanlabs.com/blog/2023/02/interfaces-101-determine-loc-with-iowriter.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Interfaces 101 : Determine LOC with io.Writer Ep. 5

Author image

Ardan Labs

February 27, 2023
interfaces-05.jpg?v=1

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK