]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: add Stringer example
authorBlain Smith <rebelgeek@blainsmith.com>
Mon, 17 Jul 2017 15:42:25 +0000 (09:42 -0600)
committerIan Lance Taylor <iant@golang.org>
Wed, 16 Aug 2017 18:02:42 +0000 (18:02 +0000)
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>

src/fmt/example_test.go [new file with mode: 0644]

diff --git a/src/fmt/example_test.go b/src/fmt/example_test.go
new file mode 100644 (file)
index 0000000..c77e788
--- /dev/null
@@ -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)
+}