编译器自动检测类型是否实现接口
经常看到一些开源库里会有一些类似下面这种奇怪的用法:
1
var _ io.Writer = (*myWriter)(nil)
Copied!
这时候会有点懵,不知道作者想要干什么,实际上这就是此问题的答案。编译器会由此检查 *myWriter 类型是否实现了 io.Writer 接口。
来看一个例子:
1
package main
2
3
import "io"
4
5
type myWriter struct {
6
7
}
8
9
/*func (w myWriter) Write(p []byte) (n int, err error) {
10
return
11
}*/
12
13
func main() {
14
// 检查 *myWriter 类型是否实现了 io.Writer 接口
15
var _ io.Writer = (*myWriter)(nil)
16
17
// 检查 myWriter 类型是否实现了 io.Writer 接口
18
var _ io.Writer = myWriter{}
19
}
Copied!
注释掉为 myWriter 定义的 Write 函数后,运行程序:
1
src/main.go:14:6: cannot use (*myWriter)(nil) (type *myWriter) as type io.Writer in assignment:
2
*myWriter does not implement io.Writer (missing Write method)
3
src/main.go:15:6: cannot use myWriter literal (type myWriter) as type io.Writer in assignment:
4
myWriter does not implement io.Writer (missing Write method)
Copied!
报错信息:*myWriter/myWriter 未实现 io.Writer 接口,也就是未实现 Write 方法。
解除注释后,运行程序不报错。
实际上,上述赋值语句会发生隐式地类型转换,在转换的过程中,编译器会检测等号右边的类型是否实现了等号左边接口所规定的函数。
总结一下,可通过在代码中添加类似如下的代码,用来检测类型是否实现了接口:
1
var _ io.Writer = (*myWriter)(nil)
2
var _ io.Writer = myWriter{}
Copied!
Copy link