sajad torkamani

Stringer is a common interface used in Go that’s defined by the fmt package:

type Stringer interface {
    String() string
}

You can define / “implement” the String() method for a custom struct:

type Person struct {
	Name string
	Age  int
}


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

So that you can then do something like:

func main() {
	john := Person{"John Doe", 42}
	bob := Person{"Bob Smith", 90}
	fmt.Println(john, bob)
}

Which should output:

John Doe (42 years) Bob Smith (90 years)
Tagged: Golang