测试接口实现
由于 Go 使用隐式接口实现,如果你的结构没有实现你打算实现的接口,则不会出现编译时错误。你可以使用类型转换显式测试实现:type MyInterface interface {Thing()
}
type MyImplementer struct {}
func (m MyImplementer) Thing() {
fmt.Println("Huzzah!")
}
// Interface is implemented, no error. Variable name _ causes value to be ignored.
var _ MyInterface = (*MyImplementer)nil
type MyNonImplementer struct {}
// Compile-time error - cannot case because interface is not implemented.
var _ MyInterface = (*MyNonImplementer)nil