在学习期间,我实际上想知道如何回到听众身边。从这个答案中,我发现了http。响应类型被传递给ServeHTTP,但事实并非如此,因为在编译时下面的代码抛出错误。这意味着不实现接口。我很好奇实现接口的类型是什么?net/http
http.Response
http.Response
http.ResponseWriter
http.ResponseWriter
# command-line-arguments
./server1.go:9:20: http.Response.Header is a field, not a method
./server1.go:9:20: impossible type assertion:
http.Response does not implement http.ResponseWriter (missing Header method)
func handler(resp http.ResponseWriter, req *http.Request){
actualresp := resp.(http.Response) //https://tour.golang.org/methods/15
resp.Write([]byte("Hello Web, from go"))
}
func main(){
http.HandleFunc("/api", handler)
http.ListenAndServe(":8000", nil)
}
慕丝7291255
相关分类