Implementing interfaces

Often we want to print a struct in a specific way.

Let's go back to the course and instructor exercise from earlier.

If we just print the course instance, we will get something that looks like JSON, but not quite.

We want to print the course information in a specific way.










The stringer interface

The way to do it is to implement the Stringer interface.










TRY IT OUT

Rob Pike


Exercise: Implement the Stringer interface on the Course struct

Attach a String() method to the course struct that prints the course information in a "nicer" way.


More information on go.dev.