Stringers

One of the most useful interfaces in Go is Stringer, defined by the fmt package:

type Stringer interface {
	String() string
}

Any type with a String() string method can describe itself as a string. The fmt package checks for this interface when printing values.

Try it: Person implements Stringer here. Change the format string in String() and watch the output update when you print a and z.

package main

import "fmt"

type Person struct {
	Name string
	Age  int
}

func (p Person) String() string {
	return fmt.Sprintf("%v (%v years)", p.Name, p.Age)
}

func main() {
	a := Person{"Arthur Dent", 42}
	z := Person{"Zaphod Beeblebrox", 9001}
	fmt.Println(a, z)
}