Skip to content

File Server

The FileServer function in the net/http package efficiently serves static files from a specified directory. It simplifies the process of serving a directory by providing a handler that serves HTTP requests with the contents of the specified directory.

Example

In this example, the FileServer function serves files from the static directory. The http.Dir function specifies the directory from which to serve files.

run command
go run src/http/file_server.go
curl localhost:8080
package main

import (
    "log"
    "net/http"
)

func main() {
    fs := http.FileServer(http.Dir("./static"))
    mux := http.NewServeMux()
    mux.Handle("/", fs)
    log.Fatal(http.ListenAndServe(":8080", mux))
}
output
<p>Corinthians</p>