package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("hello world"))
})
http.ListenAndServe(":8000", nil)
}
If I remove the *
in http.Request
:
github.com/creating_web_app_go/main.go:8: cannot use func literal (type func(http.ResponseWriter, http.Request)) as type func(http.ResponseWriter, *http.Request) in argument to http.HandleFunc
I'm very new to both Go and pointers.
So the Question is,
why must http.Request
be a pointer rather than a func literal
? Can anyone explain this in the simplest way possible, with perhaps a reference to source code?
Because it's a large struct. Copying it would be expensive. So it's a pointer to a struct, which is common in Go when structs are large. Also it has some state, so it would likely be confusing if it were copied. It would make no sense to be a func literal; I don't understand why that would be an option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With