首先,我仍然不清楚如何構(gòu)建這個(gè)問(wèn)題,但我無(wú)法理解,有人可以幫助我理解這一點(diǎn)。如果我重命名“serveHTTP”或沒(méi)有該方法,為什么下面的代碼會(huì)出錯(cuò)。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]對(duì)于下面的代碼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 回答

婷婷同學(xué)_
TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
ServeHTTP需要滿足http.Handler接口。
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
Go 中的接口提供了一種指定對(duì)象行為的方法:如果某些東西可以做到這一點(diǎn),那么它就可以在這里使用。
有關(guān)詳細(xì)信息,請(qǐng)參閱Effective Go 中的接口和類型。
- 1 回答
- 0 關(guān)注
- 178 瀏覽
添加回答
舉報(bào)
0/150
提交
取消