Stringers
type Stringer interface {
String() string
}Example
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{"Oliver Chen", 20}
z := Person{"Yulin Chen", 4}
fmt.Println(a, z)
}
Exercise
Last updated