Creating a Multi-Page Web Application in Go

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating the Web Application
  5. Creating Multiple Pages
  6. Conclusion

Introduction

In this tutorial, we will learn how to create a multi-page web application using the Go programming language. By the end of this tutorial, you will be able to build a simple web application with multiple pages that can handle user requests and serve dynamic content.

Prerequisites

Before starting this tutorial, you should have a basic understanding of Go programming language, HTML, and CSS. You should also have Go installed on your machine. If you haven’t installed Go yet, please visit the official Go website (https://golang.org) and download the latest stable version for your operating system.

Setup

Once you have Go installed, you can verify its installation by opening a terminal or command prompt and running the following command:

go version

If Go is installed correctly, it will display the version number. Now, let’s set up a project directory for our web application.

  1. Create a new directory for your project:

     mkdir mywebapp
    
  2. Change into the project directory:

     cd mywebapp
    
  3. Initialize a new Go module:

     go mod init github.com/your-username/mywebapp
    
  4. Create a main Go file:

     touch main.go
    

Creating the Web Application

Now that we have set up our project directory, let’s create a simple web application with a single page.

  1. Open the main.go file in a text editor and add the following code:

     package main
        
     import (
         "fmt"
         "net/http"
     )
        
     func main() {
         http.HandleFunc("/", homeHandler)
        
         fmt.Println("Server started on port 8080")
         http.ListenAndServe(":8080", nil)
     }
        
     func homeHandler(w http.ResponseWriter, r *http.Request) {
         fmt.Fprintf(w, "Welcome to my web application!")
     }
    

    In this code, we import the necessary packages (fmt and net/http) and define a main function. We then use http.HandleFunc to map the root path (“/”) to the homeHandler function. The homeHandler function writes a simple message to the response writer.

  2. Save the main.go file and navigate to the project directory (mywebapp) using the terminal or command prompt.

  3. Start the web server by running the following command:

     go run main.go
    

    You should see the message “Server started on port 8080” printed to the console.

  4. Open your web browser and visit http://localhost:8080. You should see the message “Welcome to my web application!” displayed on the page.

    Congratulations! You have created a basic web application using Go.

Creating Multiple Pages

Now that we have a single-page web application, let’s extend it to support multiple pages.

  1. Open the main.go file in a text editor and modify the homeHandler function as follows:

     func homeHandler(w http.ResponseWriter, r *http.Request) {
         fmt.Fprintf(w, "<h1>Welcome to my web application!</h1>")
         fmt.Fprintf(w, "<a href=\"/about\">About</a>")
     }
    

    In this code, we have added an HTML heading and a link to the /about page.

  2. Add a new handler function for the /about page:

     func aboutHandler(w http.ResponseWriter, r *http.Request) {
         fmt.Fprintf(w, "<h1>About Page</h1>")
         fmt.Fprintf(w, "<a href=\"/\">Home</a>")
     }
    
  3. Update the main function to include the new /about handler:

     func main() {
         http.HandleFunc("/", homeHandler)
         http.HandleFunc("/about", aboutHandler)
        
         fmt.Println("Server started on port 8080")
         http.ListenAndServe(":8080", nil)
     }
    
  4. Save the main.go file and restart the web server by running the following command:

     go run main.go
    
  5. Visit http://localhost:8080 in your web browser. You should see the updated home page with a link to the about page.

  6. Click on the “About” link to navigate to the /about page. You should see the heading “About Page” and a link back to the home page.

    By following these steps, you have successfully created a multi-page web application using Go.

Conclusion

In this tutorial, we learned how to create a multi-page web application in Go. We started by setting up a project directory and creating a basic web application with a single page. Then, we extended the application to support multiple pages by adding new handler functions and updating the main function.

By applying the concepts covered in this tutorial, you can continue building more complex web applications in Go. Remember to experiment with different HTML templates, CSS stylesheets, and explore additional features provided by Go’s net/http package to further enhance your web applications.