type I interface{
......
}
type T struct{}
var _ I = T{} // Verify that T implements I.
var _ I = (*T)(nil) // Verify that *T implements I.
如果T
(或*T
,相应地) 没有实现 I
,错误将在编译时被发现。
也可以直接类型断言
type WriterTo interface {
WriteTo(w Writer) (n int64, err error)
}
// If the reader has a WriteTo method, use it to do the copy.
// Avoids an allocation and a copy.
if wt, ok := src.(WriterTo); ok {
return wt.WriteTo(dst)
}