Golang http serve and help please

package main

import (“fmt”
“net/http”
)
type MyHandler struct{}
func (h *MyHandler) ServeHTTP (w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, “Hello Internet,Goodbye Local”)
}
func main() {
handler :=MyHandler{}
server :=http.Server{
Addr: “127.0.0.1:8080”,
Handler: &handler,
}
server.ListenAndServe()
}

I don’t understand for what have I written func(h *MyHandler…) if I even don’t call it in main func.How does this snippet impact to realize code.How does this method is called if I don’t do this in func main and for what I declare empty struct?
Thank you!

It’s part of the contract, http.Server will call it

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.