Routing
package main
import (
"fmt"
"net/http"
)
func main() {
mux := http.NewServeMux()
// request -> data type: struct
var handlerMain http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Fprintf(w, r.Method)
} else if r.Method == http.MethodPost {
fmt.Fprintf(w, r.Method)
} else if r.Method == http.MethodPatch {
fmt.Fprintf(w, r.Method)
} else if r.Method == http.MethodDelete {
fmt.Fprintf(w, r.Method)
}
}
mux.HandleFunc("/", handlerMain)
fmt.Println("Server running")
server := http.Server{
Addr: "localhost:5000",
Handler: mux,
}
err := server.ListenAndServe()
if err != nil{
panic(err.Error())
}
}

Last updated