aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBlain Smith <rebelgeek@blainsmith.com>2017-07-17 09:42:25 -0600
committerIan Lance Taylor <iant@golang.org>2017-08-16 18:02:42 +0000
commit58f84fdf29ca2f014a813991cf35e52de91a43cb (patch)
treeefc195af6065569067eaba68a82ab034e1ad8c17 /src
parente82e1204290ffb4b6b4953d25c5451bc2a0f2f1a (diff)
downloadgo-58f84fdf29ca2f014a813991cf35e52de91a43cb.tar.xz
fmt: add Stringer example
Change-Id: I901f995f8aedee47c48252745816e53192d4b7e4 Reviewed-on: https://go-review.googlesource.com/49090 Reviewed-by: Sam Whited <sam@samwhited.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Sam Whited <sam@samwhited.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/fmt/example_test.go29
1 files changed, 29 insertions, 0 deletions
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)
+}