From 58f84fdf29ca2f014a813991cf35e52de91a43cb Mon Sep 17 00:00:00 2001 From: Blain Smith Date: Mon, 17 Jul 2017 09:42:25 -0600 Subject: [PATCH] fmt: add Stringer example Change-Id: I901f995f8aedee47c48252745816e53192d4b7e4 Reviewed-on: https://go-review.googlesource.com/49090 Reviewed-by: Sam Whited Reviewed-by: Ian Lance Taylor Run-TryBot: Sam Whited TryBot-Result: Gobot Gobot --- src/fmt/example_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/fmt/example_test.go diff --git a/src/fmt/example_test.go b/src/fmt/example_test.go new file mode 100644 index 0000000000..c77e78809c --- /dev/null +++ b/src/fmt/example_test.go @@ -0,0 +1,29 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fmt_test + +import ( + "fmt" +) + +// Animal has a Name and an Age to represent an animal. +type Animal struct { + Name string + Age uint +} + +// String makes Animal satisfy the Stringer interface. +func (a Animal) String() string { + return fmt.Sprintf("%v (%d)", a.Name, a.Age) +} + +func ExampleStringer() { + a := Animal{ + Name: "Gopher", + Age: 2, + } + fmt.Println(a) + // Output: Gopher (2) +} -- 2.48.1