dpka7974
2016-02-19 08:42
浏览 746
已采纳

转到:http.Request中的read方法

I'm reading Go Web Programming by Sau Sheong Chang. Here is a sample code for reading data from a request body:

import (
    "fmt"
    "net/http"
)

func bodyfunc(w http.ResponseWriter, r *http.Request) {
    len := r.ContentLength
    body := make([]byte, len)
    r.Body.Read(body)
    fmt.Fprintln(w, string(body))
}

func main() {
    server := http.Server{
        Addr: "127.0.0.1:8080",
    }
    http.HandleFunc("/body", bodyfunc)
    server.ListenAndServe()
}

By definition, the Body field in the Request struct is actually an io.ReadCloser interface. My question is: The Read method in this interface is just declared but not implemented. Meanwhile the code works well. The implementation of the Read method must have been done somewhere. Where is it?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题