...

Source file src/fmt/stringer_example_test.go

Documentation: fmt

		 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  
		 5  package fmt_test
		 6  
		 7  import (
		 8  	"fmt"
		 9  )
		10  
		11  // Animal has a Name and an Age to represent an animal.
		12  type Animal struct {
		13  	Name string
		14  	Age	uint
		15  }
		16  
		17  // String makes Animal satisfy the Stringer interface.
		18  func (a Animal) String() string {
		19  	return fmt.Sprintf("%v (%d)", a.Name, a.Age)
		20  }
		21  
		22  func ExampleStringer() {
		23  	a := Animal{
		24  		Name: "Gopher",
		25  		Age:	2,
		26  	}
		27  	fmt.Println(a)
		28  	// Output: Gopher (2)
		29  }
		30  

View as plain text