I'm learning Go coming from a PHP/JS background. I came across an example where I'm not quite sure what's happening. How is timeHandler
receiving the http.ResponseWriter & http.Request if they are not explicitly passed in to the function call?
package main
import (
"log"
"net/http"
"time"
)
func timeHandler(w http.ResponseWriter, r *http.Request) {
tm := time.Now().Format(time.RFC1123)
w.Write([]byte("The time is: " + tm))
}
func main() {
mux := http.NewServeMux()
// *** Why isn't there any undefined value errors here?
th := http.HandlerFunc(timeHandler)
mux.Handle("/time", th)
http.ListenAndServe(":8080", mux)
}
The related post did not answer the question in layman enough terms. The thread in this post with Deefdragon did a good job. To summarize:
it registers the function, without calling it until later