Table of Contents
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.
-
Create a new directory for your project:
mkdir mywebapp
-
Change into the project directory:
cd mywebapp
-
Initialize a new Go module:
go mod init github.com/your-username/mywebapp
-
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.
-
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
andnet/http
) and define amain
function. We then usehttp.HandleFunc
to map the root path (“/”) to thehomeHandler
function. ThehomeHandler
function writes a simple message to the response writer. -
Save the
main.go
file and navigate to the project directory (mywebapp
) using the terminal or command prompt. -
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.
-
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.
-
Open the
main.go
file in a text editor and modify thehomeHandler
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. -
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>") }
-
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) }
-
Save the
main.go
file and restart the web server by running the following command:go run main.go
-
Visit
http://localhost:8080
in your web browser. You should see the updated home page with a link to the about page. -
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.