1// Copyright 2017 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package fmt_test 6 7import ( 8 "fmt" 9) 10 11// Animal has a Name and an Age to represent an animal. 12type Animal struct { 13 Name string 14 Age uint 15} 16 17// String makes Animal satisfy the Stringer interface. 18func (a Animal) String() string { 19 return fmt.Sprintf("%v (%d)", a.Name, a.Age) 20} 21 22func ExampleStringer() { 23 a := Animal{ 24 Name: "Gopher", 25 Age: 2, 26 } 27 fmt.Println(a) 28 // Output: Gopher (2) 29} 30