Building a Simple Web Server with Go

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

🔧 Curious Engineer with a Passion for Linux, IT, and Open Source | Embracing Innovation and Ready to Make Waves in the Tech World! 🌊
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
Buy Sendgrid Accounts If you need more info Contact us: ⏩ Telegram: @BankTrustAccount
You're most welcome! I'm delighted to hear that the approach resonated with you.
Thank you! Your support means a lot to me. I'm glad you enjoyed it. Stay tuned for more exciting content coming your way! 🙌
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.
Sure thing. Can you share what error you are getting? Connect to me on Twitter, so that we can discuss it? @ChetanThapliyal
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
Check Twitter, I have texted you. Let's solve the problem. Kanishka Verma
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. 😊📚
Thank You
Focusing on deploying, automating, and managing cloud-based applications. Explore real-world DevOps projects, tools, and best practices. (Azure and GCP)
In today's digital landscape, ensuring the authenticity and security of online communications has become increasingly important. One crucial aspect of this is verifying the legitimacy of domain names. Domain verification helps prevent phishing attack...
Maintaining uptime and monitoring system health is crucial for any organization. Real-time monitoring ensures that issues are identified and resolved quickly, minimizing downtime and maintaining service reliability. In this blog, we'll walk through a...

In the fast-paced world of software development, the ability to deploy applications efficiently and securely is a critical skill. In this project we'll explores the Application Deployment through the lens of modern DevOps practices, showcasing a powe...

In this project, we will implement Continuous Integration (CI) and Continuous Deployment (CD) for a multi-microservice application developed by docker team. This application, comprising several interconnected microservices, requires a robust and auto...

In today's software development landscape, the swift delivery of new features and updates is paramount; however, this must be balanced against the increasing importance of robust security practices. CI/CD (Continuous Integration/Continuous Delivery o...

Docker has revolutionized the way we build, package, and deploy applications. However, as projects grow in complexity, the size of Docker images can quickly balloon, leading to slower build times, increased storage requirements, and reduced performan...

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.
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.
Before you begin, make sure you have the following:
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:
Go programming language installed on your machine. You can download and install Go from the official website: https://golang.org/dl/
Let's start by creating a simple web server that serves static HTML files and handles HTTP requests. Follow these steps:
Create a new directory for your project:
mkdir WebServer-with-Go
cd WebServer-with-Go
Inside the project directory, create a subdirectory named static:
mkdir static
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
Use tree command to check directory structure:
tree
WebServer-with-Go/
├── main.go
└── static
├── form.html
├── hello.html
└── index.html
Create a file named main.go in the project directory.
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)
}
}
Open a terminal window and navigate to your project directory.
Run the following command to build and run the web server:
go run main.go
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



Let's break down the key components of the code you just created:
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.
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.
Serving Static Files: We create a file server to serve static files from the static directory using http.FileServer.
Route Handling: We use http.HandleFunc to associate our handler functions with specific routes, such as /hello and /form.
Listening and Serving: We use http.ListenAndServe to start the web server on port 8080. If an error occurs, we log it.
helloHandlerfunc 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.
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.
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.
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.
formHandlerif 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.
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.
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.
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()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.
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.
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.
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.
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.
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.
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".
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!