Go Lesson 50 – Final Golang Capstone | Dataplexa

Final Golang Capstone Project

This is the final lesson of the Golang course. In this capstone project, you will apply everything you have learned so far to build a complete, real-world backend application using Go.

The goal of this lesson is not just to write code, but to understand how a professional Go project is structured, executed, and extended.


Project Objective

You will build a RESTful Task Management API using Go. The application allows users to create and retrieve tasks using HTTP requests.

This type of application is commonly used in backend systems, microservices, and cloud-native architectures.


What This Project Demonstrates

  • Real-world Go project structure
  • Usage of packages and modules
  • HTTP server creation
  • JSON request and response handling
  • Concurrency-safe data storage

Project Folder Structure

Before writing code, it is important to understand how the project is organized. Each file has a clear responsibility.

go-task-manager/
│── main.go
│── handlers.go
│── models.go
│── store.go
│── go.mod

Explanation of files:

  • main.go – Application entry point
  • models.go – Data structures
  • store.go – In-memory data storage
  • handlers.go – HTTP request handlers
  • go.mod – Go module configuration

Step 1: Initializing the Go Module

The first step is to initialize the Go module. This enables dependency management and version control.

go mod init go-task-manager

This command creates the go.mod file and registers your project as a Go module.


Step 2: Defining the Task Model

A model represents the structure of the data your application works with. In this project, each task has an ID, title, and completion status.

package models

type Task struct {
	ID        int    `json:"id"`
	Title     string `json:"title"`
	Completed bool   `json:"completed"`
}

Explanation:

  • ID uniquely identifies each task
  • Title stores the task description
  • Completed indicates task status
  • JSON tags allow automatic JSON conversion

Step 3: Creating a Concurrency-Safe Store

The store manages all tasks in memory. A mutex is used to prevent race conditions when multiple requests occur.

package store

import (
	"sync"
	"go-task-manager/models"
)

var (
	tasks  = []models.Task{}
	mutex  sync.Mutex
	nextID = 1
)

func AddTask(title string) models.Task {
	mutex.Lock()
	defer mutex.Unlock()

	task := models.Task{
		ID:        nextID,
		Title:     title,
		Completed: false,
	}
	nextID++
	tasks = append(tasks, task)
	return task
}

func GetTasks() []models.Task {
	mutex.Lock()
	defer mutex.Unlock()
	return tasks
}

What happens here:

  • Mutex ensures safe concurrent access
  • Each task receives a unique ID
  • Tasks are stored in memory

Step 4: Handling HTTP Requests

Handlers connect HTTP requests to application logic. They parse input data and return JSON responses.

package handlers

import (
	"encoding/json"
	"net/http"
	"go-task-manager/store"
)

func CreateTask(w http.ResponseWriter, r *http.Request) {
	var input struct {
		Title string `json:"title"`
	}

	json.NewDecoder(r.Body).Decode(&input)
	task := store.AddTask(input.Title)

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(task)
}

func GetTasks(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(store.GetTasks())
}

Key points:

  • Reads JSON input from request body
  • Calls store functions
  • Returns JSON responses

Step 5: Running the HTTP Server

The main function wires everything together and starts the server.

package main

import (
	"net/http"
	"go-task-manager/handlers"
)

func main() {
	http.HandleFunc("/tasks", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodPost {
			handlers.CreateTask(w, r)
		} else if r.Method == http.MethodGet {
			handlers.GetTasks(w, r)
		}
	})

	http.ListenAndServe(":8080", nil)
}

This starts a web server on port 8080 and listens for HTTP requests.


Testing the Application

You can test the API using tools like curl or Postman.

curl -X POST http://localhost:8080/tasks \
-H "Content-Type: application/json" \
-d '{"title":"Build Golang Project"}'

What You Have Achieved

  • Built a real backend application
  • Used Go modules and packages
  • Handled HTTP requests and JSON
  • Applied concurrency safety

Course Completion

You have now completed the Golang course. With these skills, you can confidently build APIs, services, and production-ready backend systems using Go.