go中如果想要实现别人写的接口,如何保证确实实现了那个接口而不是错过了什么?
作者:互联网
在类型的实现方法上定义通用代码指令
var _ 要实现的接口 = (receiver类型)(nil)
比如要定义一个web处理程序
type handler_def struct{}
var _ http.Handler = (*handler_def)(nil)
func (h *handler_def)ServeHTTP(参数){}
当你编译go build xxxx.go
报错参考如下,可以看到很清楚的解释.
# command-line-arguments .\main.go:9:5: cannot use (*Handler)(nil) (type *Handler) as type http.Handler in assignment: *Handler does not implement http.Handler (wrong type for ServeHTTP method) have ServeHTTP(http.ResponseWriter, http.Request) want ServeHTTP(http.ResponseWriter, *http.Request)
标签:ServeHTTP,http,nil,实现,接口,Handler,go,type 来源: https://www.cnblogs.com/keystone/p/14010693.html