// Chapter 29 example: fmt.Stringer, an interface from the standard library. // Run with: go run exercises/29/05-stringer.go package main import "fmt" type point struct { x int y int } func (p point) String() string { return fmt.Sprintf("(%d, %d)", p.x, p.y) } type size struct { w int h int } func main() { p := point{x: 3, y: 4} fmt.Println(p) // (3, 4) fmt.Printf("Position: %v\n", p) // Position: (3, 4) fmt.Println([]point{{x: 1, y: 2}, {x: 5, y: 6}}) // [(1, 2) (5, 6)] // size has no String method, so it prints in the usual struct form. fmt.Println(size{w: 3, h: 4}) // {3 4} var s fmt.Stringer = p fmt.Println(s.String()) // (3, 4) }