mirror of
https://github.com/golang/go
synced 2024-11-19 14:24:47 -07:00
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>
This commit is contained in:
parent
e82e120429
commit
58f84fdf29
29
src/fmt/example_test.go
Normal file
29
src/fmt/example_test.go
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user