Writing Our Own Interfaces I

Let’s learn how to implement sort.Interface for 2D shapes.

After learning about using existing interfaces, we will write another command-line utility that sorts 3D shapes according to their volumes. This task requires learning the following tasks:

  • Creating new interfaces

  • Combining existing interfaces

  • Implementing sort.Interface for 3D shapes

Creating our own interfaces is easy. For reasons of simplicity, we include our own interface in the main package. However, this is rarely the case as we usually want to share our interfaces, which means that interfaces are usually included in Go packages other than main.

New interface definition syntax and requirements

The following code excerpt defines a new interface:

Get hands-on with 1400+ tech skills courses.