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?