首先,我仍然不清楚如何构建这个问题,但我无法理解,有人可以帮助我理解这一点。如果我重命名“serveHTTP”或没有该方法,为什么下面的代码会出错。prog.go:17: cannot use &status (type *statusHandler) as type http.Handler in argument to httptest.NewServer:*statusHandler does not implement http.Handler (missing ServeHTTP method)[process exited with non-zero status]对于下面的代码type statusHandler intfunc (s *statusHandler) aServeHTTP(w http.ResponseWriter, r *http.Request) { log.Println(" inside status handler serve http")}func main() { status := statusHandler(400) s := httptest.NewServer(&status) log.Println("value of s is %d", s) defer s.Close()}http://play.golang.org/p/QZIrWALAm_
1 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
ServeHTTP需要满足http.Handler接口。
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
Go 中的接口提供了一种指定对象行为的方法:如果某些东西可以做到这一点,那么它就可以在这里使用。
有关详细信息,请参阅Effective Go 中的接口和类型。
- 1 回答
- 0 关注
- 166 浏览
添加回答
举报
0/150
提交
取消