Talk is cheap, show me the code ^_^

package main

import (
	"fmt"
)

// PrintString : test for interface
type PrintString interface {
	String() string
}

// Person : type
type Person struct {
	Name string
}

// Animal : type
type Animal struct {
	Name string
}

func (p *Person) String() string {
	toString := "[Person<name:" + p.Name + ">]"
	return toString
}

func (a *Animal) String() string {
	toString := "[Animal<name:" + a.Name + ">]"
	return toString
}

// Print : invoke String() from PrintString interface
func Print(p interface{}) {
	if v, ok := p.(PrintString); ok {
		fmt.Printf("%v\n", v.String())
	}
}

func main() {
	p := Person{"emacsist"}
	a := Animal{"cat"}

	Print(&p)
	Print(&a)
}

输出的结果如下:

[Running] go run "/Users/emacsist/go-project/src/main.go"
[Person<name:emacsist>]
[Animal<name:cat>]

[Done] exited with code=0 in 0.724 seconds