A question about the pointer of this place

I feel that pointers are not needed at all in this place. Why do I see many related examples using pointers? There is some doubt

package main

import (
    "fmt"
    "net/http"
)

type MyMux struct {
}
//
func (p *MyMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
        sayhelloName(w, r)
        return
    }
    http.NotFound(w, r)
    return
}

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello myroute!")
}

func main() {
    mux := &MyMux{}
    http.ListenAndServe(":9090", mux)
}
Apr.01,2021

there is no need at all in your question

A pointer is needed only if you need to read struct private members, otherwise you don't need

Menu