Shine on You Crazy Interface - part2

No interface discussion is complete without talking about the fmt.Stringer interface. This is a classic when introducing the topic of interfaces.

Notice in part1 we actually made use of this interface to format the songs type.

func (s songs) String() string {
    var ss []string
    for i := range s {
        ss = append(ss, s[i].name+" "+fmt.Sprint(s[i].release.Year()))
    return strings.Join(ss, ", ")

When we called a print function the output was nicely formatted:

// Comfortably Numb 1979, Interstellar Overdrive 1967, Time 1973, High Hopes 1994

If we remove the above String() method the output for the song type would be:

// [{Comfortably Numb {0 62445772800 }} {Interstellar Overdrive {0 62059132800 }} {Time {0 62235302400 }} {High Hopes {0 62897990400 }}]

So let’s take a step back and look at the fmt.Stringer interface:

type Stringer interface {
    String() string

To satisfy the Stringer interface our concrete type must have a method with the String() string signature, which we already saw above.

By satisfying the Stringer interface we can take advantage of functions throughout the standard library that leverage this interface to print values. Most notably this will be used with the fmt package, where we call fmt.Println and the output will be formatted according to our representation of the value.

TODO(mf): WIP…