在类型的实现方法上定义通用代码指令
var _ 要实现的接口 = (receiver类型)(nil)
比如要定义一个web处理程序
type handler_def struct{}
var _ http.Handler = (*handler_def)(nil) //(*handler_def)(nil) 构建指针/切片/map的零值
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)