Learning Go is exciting. Decided to see how to set up a Web Application or API using Go language along with FastAPI implementation.
package main
import (
"log"
"net/http"
)
// Define a home handler function
func home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello from Goooooo"))
}
func main() {
// http.NewServeMux() function to initialize a new servemux
mux := http.NewServeMux()
// match "/" URL pattern with home handler function
mux.HandleFunc("/", home)
// http.ListenAndServe() function to start a new web server
err := http.ListenAndServe(":4000", mux)
log.Fatal(err)
}
As usual: “go run .”
Here we gooooo:
in servemux the pattern
"/"
= a catch-all.