系统接口:
type Stringer interface {
String() string
}
例子:
import "fmt"
type Test struct {
m int
}
func (t *Test) String() string {
return "kshf"
}
func main() {
t := &Test{}
//会打印t.String()
fmt.Println(t)
}
另外连个很重要的io接口
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
很多系统自带和第三方库的关于io的结构体都实现了这两个接口,因为很多系统函数或第三方函数的参数就是Writer或Reader,本质上还是多态的使用