【发布时间】:2021-02-13 23:02:50
【问题描述】:
这段代码我真的看不懂
我想我理解接收器是如何工作的,但是当 /foo 被请求时,为什么该结构成为 SERVEHTTP 的接收器,而它从未被调用?如果这是 http 的工作方式,那么这是典型的接收器样式使用吗?
main go
import "net/http"
type fooHandler struct {
Message sstring
}
func (f *fooHandler) ServerHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(f.Message))
}
func main() {
http.Handle("/foo", &fooHandler{Message: "Hello world"})
}
【问题讨论】:
-
这可能对servehttp-work有帮助
-
谢谢 LINK 太好了@abhijitgupta 我觉得我现在理解得更好了.. 谢谢。
标签: go