Skip to main content

Command Palette

Search for a command to run...

Building a Simple Web Server with Go

Updated
8 min read
Building a Simple Web Server with Go
C

🔧 Curious Engineer with a Passion for Linux, IT, and Open Source | Embracing Innovation and Ready to Make Waves in the Tech World! 🌊

Are you a beginner looking to learn about building a basic web server using the Go programming language? In this tutorial, we'll walk you through creating a straightforward web server that serves static files and handles basic HTTP requests. By the end of this tutorial, you'll have a solid understanding of the key concepts involved in building a web server with Go.

What is a Web Server?

A web server is a software application that handles incoming requests from clients (typically web browsers) and responds by serving web content. When you type a URL into your web browser and hit Enter, your browser sends a request to a web server, which processes the request and sends back the appropriate response. This response can be a web page, an image, a file, or even dynamic content generated by the server.

Web servers play a crucial role in the architecture of the World Wide Web, as they facilitate the exchange of information between clients and servers. They are fundamental building blocks for serving websites, web applications, and other online services.

Prerequisites

Before you begin, make sure you have the following:

  1. Basic understanding of Go programming concepts.

    You can refer to this repo for Basics: https://github.com/ChetanThapliyal/get-started-with-Go/tree/main
    OR
    Read the Go blog Series:

    https://tech-transitions.hashnode.dev/series/learn-golang

  2. Go programming language installed on your machine. You can download and install Go from the official website: https://golang.org/dl/

Getting Started

Let's start by creating a simple web server that serves static HTML files and handles HTTP requests. Follow these steps:

Step 1: Setting Up the Project

  1. Create a new directory for your project:

     mkdir WebServer-with-Go
     cd WebServer-with-Go
    
  2. Inside the project directory, create a subdirectory named static:

     mkdir static
    
  3. Create two HTML files named index.html and form.html inside the static directory. You can use any text editor to create these files and add basic HTML content.

     cd static
     touch index.html form.html hello.html
    

    Visit this to populate the above files (copy the code from the link mentioned below and paste in the above files):

    form.html : https://github.com/ChetanThapliyal/WebServer-wih-Go/blob/main/static/form.html

    index.html: https://github.com/ChetanThapliyal/WebServer-wih-Go/blob/main/static/index.html
    hello.html : https://github.com/ChetanThapliyal/WebServer-wih-Go/blob/main/static/hello.html

  4. Use tree command to check directory structure:

     tree
     WebServer-with-Go/
     ├── main.go
     └── static
         ├── form.html
         ├── hello.html
         └── index.html
    

Step 2: Writing the Go Code

  1. Create a file named main.go in the project directory.

  2. Open main.go in a text editor and copy the following code:

     package main
    
     import (
         "fmt"
         "log"
         "net/http"
     )
    
     func helloHandler(w http.ResponseWriter, r *http.Request) {
         if r.URL.Path != "/hello" {
             http.Error(w, "404 Not Found", http.StatusNotFound)
             return
         }
         if r.Method != "GET" {
             http.Error(w, "Method is not supported", http.StatusNotFound)
             return
         }
         fmt.Fprintf(w, "Hello there")
     }
    
     func formHandler(w http.ResponseWriter, r *http.Request) {
         if err := r.ParseForm(); err != nil {
             fmt.Fprintf(w, "ParseForm() err: %v", err)
             return
         }
         fmt.Fprintf(w, "POST Request Successful\n")
         name := r.FormValue("name")
         address := r.FormValue("address")
         fmt.Fprintf(w, "Name = %s\n", name)
         fmt.Fprintf(w, "Address = %s", address)
     }
    
     func main() {
         fileServer := http.FileServer(http.Dir("./static"))
         http.Handle("/", fileServer)
         http.HandleFunc("/form", formHandler)
         http.HandleFunc("/hello", helloHandler)
    
         fmt.Printf("Starting server at port 8080\n")
    
         if err := http.ListenAndServe(":8080", nil); err != nil {
             log.Fatal(err)
         }
     }
    

Step 3: Running the Web Server

  1. Open a terminal window and navigate to your project directory.

  2. Run the following command to build and run the web server:

     go run main.go
    
  3. Open your web browser and visit http://localhost:8080 to see your web server in action. You should be able to see the following:

    localhost:8080
    localhost:8080/hello.html
    localhost:8080/form.html

General Understanding of the Code

Let's break down the key components of the code you just created:

  1. Importing Packages: We import necessary packages like

    fmt: Package for formatted I/O (used for printing messages).

    log: Package for logging.

    net/http Package for building HTTP servers & for handling HTTP requests and responses.

  2. Handlers: We define two handler functions,

    helloHandler: Handles requests to the "/hello" route. It checks if the URL path is "/hello" and if the HTTP method is "GET". If not, it returns a 404 error. If everything is in order, it responds with "Hello there".

    formHandler: Handles POST requests to the "/form" route. It parses the form data from the request, extracts the values for the "name" and "address" fields, and then responds with a success message along with the extracted data.

    to handle specific paths and HTTP methods.

  3. Serving Static Files: We create a file server to serve static files from the static directory using http.FileServer.

  4. Route Handling: We use http.HandleFunc to associate our handler functions with specific routes, such as /hello and /form.

  5. Listening and Serving: We use http.ListenAndServe to start the web server on port 8080. If an error occurs, we log it.

Detailed Understanding of Functions

helloHandler

  1. func helloHandler(w http.ResponseWriter, r *http.Request): This line defines the function with two parameters:

    • w http.ResponseWriter: This parameter allows you to write the HTTP response back to the client.

    • r *http.Request: This parameter holds information about the incoming HTTP request.

  2. if r.URL.Path != "/hello" { ... }: This line checks whether the URL path of the incoming request (r.URL.Path) is not equal to "/hello". If it's not, it means the request is not intended for the "/hello" route. In that case, the server responds with a "404 Not Found" error and returns early.

  3. if r.Method != "GET" { ... }: This line checks whether the HTTP method used in the request (r.Method) is not "GET". If the method is not "GET", it means that the "/hello" route only supports GET requests. If another method is used (like POST or PUT), the server responds with a "Method is not supported" error and returns early.

  4. fmt.Fprintf(w, "Hello there"): If the URL path is "/hello" and the HTTP method is "GET", this line writes the response "Hello there" to the w (ResponseWriter) parameter, which sends the response back to the client.

formHandler

  1. if err := r.ParseForm(); err != nil { ... }: This line parses the form data from the incoming request (r) and populates the r.Form map with the form values. If there's an error during parsing, it means the form data is not correctly structured, and the server responds with an error message indicating the parsing error.

  2. fmt.Fprintf(w, "POST Request Successful\n"): After successfully parsing the form data, the server writes "POST Request Successful" to the response. This indicates that the form submission was received and processed successfully.

  3. name := r.FormValue("name") and address := r.FormValue("address"): These lines extract the values of the "name" and "address" fields from the parsed form data. The r.FormValue function is used to retrieve the value associated with a specific form key.

  4. fmt.Fprintf(w, "Name = %s\n", name) and fmt.Fprintf(w, "Address = %s", address): These lines write the extracted "name" and "address" values to the response. The %s placeholder is replaced with the actual values.

main()

  1. fileServer := http.FileServer(http.Dir("./static")): This line creates an HTTP file server (fileServer) that serves static files from the "./static" directory. The http.Dir("./static") part indicates the directory from which the files will be served.

  2. http.Handle("/", fileServer): This line sets up a handler for the root ("/") route. Any request to the root route will be served by the fileServer created in the previous step, meaning that static files from the "./static" directory will be served when you access the root URL.

  3. http.HandleFunc("/form", formHandler): This line sets up a handler for the "/form" route. When a request is made to the "/form" route, the formHandler function we discussed earlier will be called to handle the request.

  4. http.HandleFunc("/hello", helloHandler): Similarly, this line sets up a handler for the "/hello" route. Requests to the "/hello" route will be handled by the helloHandler function.

  5. fmt.Printf("Starting server at port 8080\n"): This line prints a message indicating that the server is starting. You'll see this message in the console when you run the program.

  6. if err := http.ListenAndServe(":8080", nil); err != nil { ... }: This line starts the HTTP server on port 8080. The http.ListenAndServe(":8080", nil) function call does the following:

    • It listens for incoming HTTP requests on port 8080.

    • It uses the handlers you set up earlier (for "/", "/form", and "/hello") to route and handle the requests.

If there's an error during server startup or while serving requests, the log.Fatal(err) line will print the error and terminate the program.

Usage

When this program is executed, it starts an HTTP server on port 8080. Here's what it does for different routes:

  • Visiting the root ("/") route serves static files from the "./static" directory.

  • Accessing the "/form" route displays a simple HTML form. Upon submitting the form, it processes the data and displays the "name" and "address" values.

  • Accessing the "/hello" route responds with "Hello there".

Conclusion

Congratulations! You've successfully created a simple web server using the Go programming language. You've learned about serving static files, handling different HTTP requests, and setting up routes. This project provides a foundation for building more complex web applications and APIs using Go. Feel free to explore and expand upon this project to learn more about web development with Go.

Remember, practice makes perfect! Experiment with the code, add new features and explore the Go documentation to deepen your understanding of web development and the Go programming language. Happy coding!

B

Buy Verified TransferWise Account (Wise) Buy Verified Wise Accounts, 100% Safe With Documents We provide the best quality fully verified UK, US, and CA Accounts. Email & phone numbers, ID Card and Bank verified and Credit Card verified Wise accounts buy those safe accounts from us.

We are working with the largest team and we are instant start work after your place order. So, Buy our Service and enjoy it..

Our Service Quality ⇒ Email Access ⇒ Account Access ⇒ Bank Verified ⇒ Bank Statement copy provided ⇒ Account Access ⇒ Phone confirmed and has access ⇒ Date of Birth Provided ⇒ Documents ( Nid card scan copy+ utility invoice scan copy) ⇒ Full Verified Wise Account (Fresh, Old, History 3 kinds of account we sell) ⇒ Money-Back Guarantee ⇒ 24/7 Customer Support

24/7 Ready to Contact:- ⏩ Skype: BankTrustAccount ⏩ Telegram: @BankTrustAccount

B

Buy Sendgrid Accounts If you need more info Contact us: ⏩ Telegram: @BankTrustAccount

A

helpful. Thanks!

2
P

I really like the way you made it very simple. Thank you very much!

1
C

You're most welcome! I'm delighted to hear that the approach resonated with you.

J

Fantastic!

1
C

Thank you! Your support means a lot to me. I'm glad you enjoyed it. Stay tuned for more exciting content coming your way! 🙌

V

Awesome series really liked it very helpful 👍

1
K

Hey, that's a great blog you've published. But actually whenever i run the main.go file it shows me error even if i change the port number. May you please help me with this.

I'm using wsl as a terminal.

1
C

Sure thing. Can you share what error you are getting? Connect to me on Twitter, so that we can discuss it? @ChetanThapliyal

K

Thanks for the reply. Actually I'm not been able to DM you on twitter.

Whenever I run the go file and visit to localhost:8080. It only shows "404 page not found". Even if change the port number it shows the same error. Chetan Thapliyal

C

Check Twitter, I have texted you. Let's solve the problem. Kanishka Verma

C
Claud2y ago

Hey this is great! Just saw how much content you’ve published on Go and I look forward to catch up on your blog posts 📚

1
C

Thank you so much for your kind words! I really appreciate your support. I'm thrilled that you're interested in my Go-related content. Looking forward to sharing more insightful blog posts with you. Feel free to reach out if you have any questions or topics you'd like me to cover. 😊📚

1
M

Helpful!

2
C

Thank You

More from this blog

TechTransitions

30 posts

🛠️ Mechanical Engineer turned IT and DevOps enthusiast with a passion for all things Open Source! From designing tangible machinery to sculpting the digital landscape. 🖥️