Function 类型如何间接引用其他函数基方法?

Function 类型如何间接引用其他函数基方法?,function,types,interface,go,Function,Types,Interface,Go,首先,我还不清楚如何界定这个问题,但我无法理解,有人能帮助我理解这个问题吗。如果我重命名“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 ServeHT

首先,我还不清楚如何界定这个问题,但我无法理解,有人能帮助我理解这个问题吗。如果我重命名“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 int

func (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()
}

ServeHTTP
是满足接口要求所必需的

type Handler interface {
        ServeHTTP(ResponseWriter, *Request)
}
Go中的接口提供了一种指定对象行为的方法:如果有什么东西可以做到这一点,那么可以在这里使用它


有关更多信息,请参阅。

谢谢您指点我,我验证了http.handler在哪里使用,我看到了它是一个参数,因为我知道传递的参数应该是类型,应该包括方法。@KishoreVaishnav
http.handler
可以在许多地方使用。最常见的可能是
http.Handle
()函数,但是看看上的索引,您会发现它们在许多其他方面都有使用。它们是许多web服务的基本构建块。它们接受请求
http.Request
,对它们进行处理,然后在
http.ResponseWriter
上发送响应。事实上,我想了解更多,你能告诉我为什么这并不真正担心CreateTable函数,即使它与Abc接口相关联?