Originally posted on Medium
Project Go: From Zero to Deploy -1
I will not waste time of yours doing intricacies of greetings.
Lets BUILD
Our First Project is to build a Web Server-> A simple yet effective project to learn Go fast
Let’s break down a small Go web server project that includes:
- A static file server
- A form handler
- A route that says hello
And we’ll do it all while explaining the syntax, what functions are doing, how handlers work, and throwing in a bit of humor to keep your coffee warm.
🧠 The Big Picture
You’ll be writing a Go program that:
- Serves static files from a static/ folder
- Responds to /hello with “Hello, World!”
- Accepts form submissions via /form
- Teaches you Go HTTP handling like it’s a cooking recipe — but with less fire
🛠️ The Code
Let’s start with the complete main.go file:
package main
import (
"fmt"
"log"
"net/http"
)
📦 package main
This tells Go, “Hey, I’m an executable program, not just a library.” Like shouting “main stage!” at a concert—>>this is where the magic begins.
📚 The Imports
- fmt: Used for formatted I/O (like printf in C, but friendlier).
- log: For logging errors and pretending you have everything under control.
- net/http: This is your golden ticket to build web servers.
🔧 Creating Our Handlers
func helloHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hello" {
http.Error(w, "Page not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
fmt.Fprintf(w, "Hello, World!")
}
🧠 Breaking It Down
- func helloHandler(w http.ResponseWriter, r *http.Request) This is a handler function. It’s like your waiter — it gets the request (r) and brings a response (w). Go passes these in automatically.
- r.URL.Path != "/hello" You're making sure people knock on the correct door. If they don’t type /hello, we slam the door politely with a 404.
- r.Method != "GET" Only allow GET requests — no POST, PUT, or carrier pigeons.
- fmt.Fprintf(w, "Hello, World!") This writes directly to the response, like saying something back to the browser.
✉️ The Form Handler
func formHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "Error parsing form: %v", err)
return
}
fmt.Fprintf(w, "Form submitted successfully!\n")
fmt.Fprintf(w, "Name: %s\n", r.FormValue("name"))
fmt.Fprintf(w, "Address: %s\n", r.FormValue("address"))
}
🍕 What’s Happening Here?
- r.ParseForm() Turns your form submission into a nice readable map. Like unwrapping a burrito and seeing the layers. Tasty.
- r.FormValue("name") Grabs the form field named “name”. If this were an email form, you’d grab it like: r.FormValue("email").
📁 Static File Server
fileServer := http.FileServer(http.Dir("Web-server/static"))
http.Handle("/", fileServer)
This line tells Go:
“Hey, anything that’s not /hello or /form, just try to serve it from the static directory.”
So if you go to localhost:8080/index.html, you’ll see your homepage!
🧷 Binding Handlers to Routes
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)
This is like putting up signposts in your app:
- If someone hits /form, call the formHandler.
- If they hit /hello, call the helloHandler.
🏁 Starting the Server
fmt.Println("Starting server on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
- :8080 means “listen on port 8080” on all available IPs.
- nil means: use the default multiplexer (like default traffic control).
- If something goes wrong, log.Fatal(err) will complain loudly and stop everything. It’s Go’s version of flipping the table.
🧪 The HTML Pages
index.html
<h2>Welcome to the Static Website</h2>
Place this inside your Web-server/static directory.
form.html
<form method="POST" action="/form">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="address">Address:</label>
<input type="text" id="address" name="address" required>
<br>
<button type="submit">Submit</button>
</form>
Submit this form and boom — your form handler catches it and writes it back.
🧩 Folder Structure
Web-server/
├── static/
│ ├── index.html
│ └── form.html
├── main.go
🎉 Done!
Now run your app:
go run main.go
Then Visit:
- http://localhost:8080/index.html → See your homepage
- http://localhost:8080/form.html → Submit a form
- http://localhost:8080/hello → Get greeted by your server
Top comments (0)