type server struct{}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "hello world"}`))
}
You are talking about this part I believe.
For http.Handle it just needs some type that implements the handler interface.
For example, this below is valid code.
type hotdog int
func (s *hotdog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "hello world"}`))
}
func main() {
var s hotdog
http.Handle("/", &s)
log.Fatal(http.ListenAndServe(":8080", nil))
}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
bro thanks for the article however why did u did not add any code in the stuct called server initially ??
You are talking about this part I believe.
For http.Handle it just needs some type that implements the handler interface.
For example, this below is valid code.