I have this code that will give my custom 404 error message if I go to /time/ but if I go to /times/ or just / or /whatever then I will get the default 404 error message. I want to show my custom 404 for all cases other than /time/
package main
import (
"fmt"
"time"
"flag"
"os"
"net/http"
)
const AppVersion = "timeserver version: 3.0"
func timeserver(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/time/" {
NotFoundHandler(w, r)
return
}
const layout = "3:04:05 PM"
t := time.Now().Local()
fmt.Fprint(w, "<html>
")
fmt.Fprint(w, "<head>
")
fmt.Fprint(w, "<style>
")
fmt.Fprint(w, "p {font-size: xx-large}
")
fmt.Fprint(w, "span.time {color: red}
")
fmt.Fprint(w, "</style>
")
fmt.Fprint(w, "</head>
")
fmt.Fprint(w, "<body>
")
//fmt.Fprint(w, "The time is now " + t.Format(layout))
fmt.Fprint(w, "<p>The time is now <span class=\"time\">" + t.Format(layout) + "</span>.</p>
")
fmt.Fprint(w, "</body>
")
fmt.Fprint(w, "</html>
")
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "custom 404")
}
func main() {
version := flag.Bool("V", false, "prints current version")
port := flag.String("port", "8080", "sets service port number")
flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
http.HandleFunc("/time/", timeserver)
http.ListenAndServe("localhost:" + *port, nil)
}