package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") }) http.ListenAndServe(":8080", nil) }
package main import ( "fmt" "net/http" ) func Middleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Printf("Received request: %s %s\n", r.Method, r.URL.Path) next.ServeHTTP(w, r) }) } func main() { mux := http.NewServeMux() // Add middleware mux.Handle("/", Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") })))) http.ListenAndServe(":8080", mux) }
package main import ( "net/http" ) func main() { http.Handle("/", http.FileServer(http.Dir("./static"))) http.ListenAndServe(":8080", nil) }This example serves static files from a directory called "static" using the http.FileServer function provided by the net/http package. Requests to the root path will return the index.html file in the static directory, and other files will be served based on their file path relative to the static directory.