Introduction

Let’s get an overview of reflection and interfaces.

Introduction to reflection and interfaces

Let’s recall the phone book application discussed previously. We might wonder how to deal with a new requirement where we want to sort user-defined data structures, such as phone book records, based on our own criteria, such as a surname or first name. What is the best way to sort different datasets that share some common behavior without having to implement sorting from scratch for each one of the different data types using multiple functions?

Now imagine that we have a utility like our phone book application that can process two different formats of CSV data files based on the given input file. Each kind of CSV record is stored in a different Go structure, which means that each dataset of CSV records might have to be sorted differently. How do we implement that without having to write two different command-line utilities?

Lastly, imagine that we want to write a utility that sorts really unusual data. For example, imagine that we have a slice that holds information on various kinds of 3D shapes, and we want to sort them based on their volume. Can this be performed easily and in a way that makes sense?

Get hands-on with 1200+ tech skills courses.