Go Lesson 49 – Web App Project | Dataplexa

Web App Project in Go

In this lesson, you will build a real-world web application using Go. This project combines everything you have learned so far, including HTTP handling, routing, templates, concurrency, and structured data.

By the end of this lesson, you will have a functional Go web server that serves dynamic content and handles multiple requests efficiently.


Project Overview

We will build a Simple Task Management Web App that:

  • Runs an HTTP server
  • Handles multiple routes
  • Displays data using HTML templates
  • Processes user input
  • Uses Go’s concurrency model implicitly via HTTP

Real-World Use Case

This type of architecture is commonly used in:

  • Microservices
  • Internal dashboards
  • Admin panels
  • API-driven web applications

Project Structure

Create the following files:

web-app/
├── main.go
├── templates/
│   └── index.html

Step 1: Basic HTTP Server

Create the main Go file and start an HTTP server.

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", homeHandler)

    fmt.Println("Server running at http://localhost:8080")
    http.ListenAndServe(":8080", nil)
}

Step 2: Create a Handler Function

Handlers process incoming HTTP requests.

func homeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Welcome to Go Web App")
}

At this stage, visiting the URL displays a simple message.


Step 3: Using HTML Templates

Templates allow dynamic HTML rendering.

<!-- templates/index.html -->
<!DOCTYPE html>
<html>
<head>
  <title>Go Task App</title>
</head>
<body>
  <h1>Task Dashboard</h1>
  <ul>
    {{range .}}
      <li>{{.}}</li>
    {{end}}
  </ul>
</body>
</html>

Step 4: Render Template from Go

Now update the handler to load and render the template.

import (
    "html/template"
    "net/http"
)

func homeHandler(w http.ResponseWriter, r *http.Request) {
    tasks := []string{
        "Build Go Web App",
        "Write API",
        "Deploy Service",
    }

    tmpl := template.Must(template.ParseFiles("templates/index.html"))
    tmpl.Execute(w, tasks)
}

How Concurrency Works Here

Go automatically handles each HTTP request in a separate goroutine. This means:

  • Multiple users can access the app simultaneously
  • No manual goroutine management is required
  • Go scales naturally with traffic

Handling User Input (POST Requests)

Add support for form submissions.

func addTaskHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method == http.MethodPost {
        task := r.FormValue("task")
        fmt.Println("New Task:", task)
    }
}

This allows users to submit data to the server.


Error Handling Best Practices

  • Always validate user input
  • Check template execution errors
  • Use proper HTTP status codes
  • Log server errors for debugging

Improvement Ideas

  • Add a database (PostgreSQL or MySQL)
  • Use middleware for logging
  • Implement authentication
  • Convert to REST API

Practice Exercises

Exercise 1

Add a form to create new tasks dynamically.

Exercise 2

Add a route to delete tasks.

Exercise 3

Serve static files like CSS.


Key Takeaways

  • Go is excellent for web development
  • HTTP servers are simple and fast
  • Concurrency is built into the web layer
  • Templates enable dynamic content

What’s Next?

In the final lesson, you will build a complete Golang Capstone Project that combines APIs, concurrency, databases, and deployment.