Golang

 

Introduction

Managing personal finances is crucial in today’s fast-paced world. An expense tracker is an essential tool to help individuals monitor their spending, set budgets, and categorize expenses to ensure financial stability. In this tutorial, we’ll create a simple expense tracker in Go that can track and categorize personal expenses. This will enable you to easily manage your income and spending.

Objective

The objective of this project is to build an Expense Tracker application using the Go programming language. The tracker will allow users to input their expenses, categorize them, and view their total spending within different categories. Users will also be able to store and display these expenses in a structured format.

Go Code for Expense Tracker


package main

import (
	"fmt"
	"time"
)

// Expense structure to hold data about each expense
type Expense struct {
	Name     string
	Amount   float64
	Category string
	Date     time.Time
}

// Function to add an expense
func addExpense(expenses []Expense, name string, amount float64, category string) []Expense {
	expense := Expense{
		Name:     name,
		Amount:   amount,
		Category: category,
		Date:     time.Now(),
	}
	return append(expenses, expense)
}

// Function to display all expenses
func displayExpenses(expenses []Expense) {
	fmt.Println("\nList of all expenses:")
	for _, expense := range expenses {
		fmt.Printf("Name: %s, Amount: %.2f, Category: %s, Date: %s\n", expense.Name, expense.Amount, expense.Category, expense.Date.Format("2006-01-02 15:04:05"))
	}
}

// Function to display total expenses by category
func displayTotalByCategory(expenses []Expense) {
	categoryTotals := make(map[string]float64)

	for _, expense := range expenses {
		categoryTotals[expense.Category] += expense.Amount
	}

	fmt.Println("\nTotal expenses by category:")
	for category, total := range categoryTotals {
		fmt.Printf("Category: %s, Total: %.2f\n", category, total)
	}
}

func main() {
	var expenses []Expense

	// Adding some expenses
	expenses = addExpense(expenses, "Coffee", 5.50, "Food")
	expenses = addExpense(expenses, "Gym Membership", 50.00, "Health")
	expenses = addExpense(expenses, "Groceries", 30.75, "Food")
	expenses = addExpense(expenses, "Internet Bill", 60.00, "Bills")

	// Display all expenses
	displayExpenses(expenses)

	// Display total by category
	displayTotalByCategory(expenses)
}
            

Explanation of the Program Structure

This Go program tracks and categorizes personal expenses. It contains the following key components:

  • Expense struct: A structure that holds information about each expense such as the name, amount, category, and date.
  • addExpense function: This function adds a new expense to the list, with parameters for the expense name, amount, and category.
  • displayExpenses function: It prints all the expenses with their details, including name, amount, category, and date.
  • displayTotalByCategory function: This function calculates and displays the total spending in each expense category.
  • main function: The entry point of the program where expenses are added and the display functions are called to show the results.

How to Run the Program

  1. Install Go on your system if it is not already installed. You can download Go from the official website: https://golang.org/dl/
  2. Create a new Go file (e.g., expense_tracker.go) and paste the code above into the file.
  3. Open a terminal or command prompt, navigate to the folder where your Go file is saved.
  4. Run the program by executing the command: go run expense_tracker.go.
  5. You will see a list of all expenses followed by the total expenses categorized by type.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)