Writing Our Own Interfaces II
Explore how to implement the Shape3D interface in Go for sorting cubes, cuboids, and spheres by their volume. Learn to use a single slice of interface types to store different shapes and apply Go's sort.Interface to order them. Understand the use of random dimension generation and customized output formatting to enhance your Go programming skills with interfaces.
We'll cover the following...
Implementing sort.Interface for 3D shapes
In this lesson, we will create a utility for sorting various 3D shapes based on their volume, which clearly illustrates the power and versatility of Go interfaces. This time, we will use a single slice for storing all kinds of structures that all satisfy a given interface. The fact that Go considers interfaces as data types allows us to create slices with elements that satisfy a given interface without getting any error messages.
This kind of scenario can be useful in various cases because it illustrates how to store elements with different data types that all satisfy a common interface on ...