go stringer_example_test 源码

2022-07-15 浏览 (781)

golang stringer_example_test 代码

文件路径:/src/fmt/stringer_example_test.go

// 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)
}

相关信息

go 源码目录

相关文章

go doc 源码

go errors 源码

go errors_test 源码

go example_test 源码

go export_test 源码

go fmt_test 源码

go format 源码

go gostringer_example_test 源码

go print 源码

go scan 源码

  • 所属分类: 后端技术
  • 本文标签: golang
  • 版权声明: 本文链接 https://seaxiang.com/blog/cb350a793b83421e9180ef9b6a7bbeb6