Serving HTML Files with Go’s net/http Package

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating an HTML File
  5. Serving HTML File
  6. Running the Application
  7. Conclusion


Introduction

In this tutorial, we will learn how to serve HTML files using Go’s net/http package. The net/http package provides a powerful and flexible framework for building web applications in Go. By the end of this tutorial, you will be able to create a simple HTTP server that serves static HTML files.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of the Go programming language and have Go installed on your machine. If you don’t have Go installed, you can download it from the official Go website (https://golang.org/dl/).

Setup

Before we begin, let’s set up the project structure. Create a new directory for your project and navigate to it in the terminal. Inside the project directory, create a new file called main.go which will contain our Go code.

$ mkdir html-server
$ cd html-server
$ touch main.go

Creating an HTML File

To serve an HTML file, we first need an HTML file to serve. Create a new file called index.html in the project directory with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>My HTML Page</title>
</head>
<body>
    <h1>Welcome to My HTML Page</h1>
    <p>This is a sample HTML file served by Go.</p>
</body>
</html>

Serving HTML File

Now let’s write the Go code to serve the HTML file. Open the main.go file in your favorite text editor and add the following code:

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "index.html")
    })

    http.ListenAndServe(":8080", nil)
}

Here’s what’s happening in the code:

  • The http.HandleFunc function is used to register a handler function for a given URL pattern. In this case, we’re using the root URL (“/”) to serve our HTML file.
  • Inside the handler function, we use http.ServeFile to serve the index.html file. The ServeFile function automatically sets the appropriate content type based on the file’s extension.
  • Finally, we call http.ListenAndServe to start the HTTP server on port 8080.

Running the Application

To run the application, open your terminal, navigate to the project directory, and execute the following command:

$ go run main.go

You should see the following output:

Listening on :8080...

Open your web browser and visit http://localhost:8080. You should see the HTML page rendered in your browser.

Conclusion

In this tutorial, we learned how to serve HTML files with Go’s net/http package. We created a simple Go application that serves a static HTML file and tested it by running a local HTTP server. You can now use this knowledge to extend your Go web applications and serve dynamic content to your users.

By following this tutorial, you should now have a good understanding of serving HTML files with Go and be able to apply this knowledge to your own projects. Happy coding!