Menu Close

Go – HTTP FileServer Function in Go

In this article, we will explore the http FileServer function in Go net/http package in detail, along with examples.

Introduction:

The Go programming language, also known as Golang, is an increasingly popular choice for building web applications, APIs, and servers. One of its strengths lies in its standard library, which includes the net/http package, providing a wide range of tools and utilities for creating HTTP servers and clients. In this article, we’ll explore the FileServer function in the net/http package, which enables you to serve static files efficiently and easily. We will walk you through a detailed example of how to use this function to create a static file server.

Overview of the http.FileServer() Function

The FileServer function in the net/http package allows you to serve static files from a directory. The function takes an http.FileSystem interface as an argument, which is usually an instance of the http.Dir type representing a file system path. The FileServer function returns an http.Handler that can be used to handle incoming HTTP requests.

Example:

package main

import (
    "log"
    "net/http"
)

func main() {
    // Set the directory to serve files from
    fileSystem := http.Dir("./static")

    // Create a FileServer handler
    fileServer := http.FileServer(fileSystem)

    // Set the endpoint for serving files
    http.Handle("/", fileServer)

    // Start the HTTP server
    log.Println("Starting server on :8080")
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

In this example, we first create an http.Dir instance pointing to the ./static directory, which contains the files we want to serve. Next, we create a file server handler by calling the http.FileServer function with the fileSystem variable. We then use the http.Handle function to set the endpoint for serving files from the root path (“/”). Finally, we start the HTTP server on port 8080 using the http.ListenAndServe function.

To test the file server, create a directory named static in the same folder as your Go source code file and add some files to it, such as an index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Go FileServer Example</title>
</head>
<body>
    <h1>Welcome to the Go FileServer Example</h1>
    <p>This page is served using the FileServer function from the net/http package in Go.</p>
</body>
</html>

Now, run the Go program, and open your browser to http://localhost:8080. You should see the contents of the index.html file.

Conclusion

The FileServer function in Go’s net/http package offers an easy and efficient way to serve static files from a directory. In this article, we demonstrated how to create a simple static file server using the FileServer function. This can be particularly useful when you need to serve assets like images, stylesheets, or scripts alongside your web application or API.

To check more Go related articles. Pls click given below link:

https://pkg.go.dev/net/http#section-documentation

Posted in golang, net, packages

Leave a Reply

Your email address will not be published. Required fields are marked *