Golang
Golang

 

Introduction

This tutorial will guide you through creating a simple Bank Account Simulator using the Go programming language. The main objective is to simulate basic banking functionalities such as deposits, withdrawals, and checking account balances. You’ll learn how to manage a simple bank account with user input and make updates to the account based on the user’s actions.

Objective

Our goal is to create a basic bank system that supports the following operations:

  • Deposit money into the account
  • Withdraw money from the account
  • Check the current balance of the account

The program will allow the user to interact with these functionalities in a loop, demonstrating a simple, real-world application of a bank account simulator.

Code for Bank Account Simulator in Go

package main

import (
    "fmt"
    "errors"
)

type BankAccount struct {
    balance float64
}

func (account *BankAccount) Deposit(amount float64) {
    if amount <= 0 {
        fmt.Println("Invalid deposit amount. Please enter a positive value.")
        return
    }
    account.balance += amount
    fmt.Printf("Successfully deposited $%.2f. New balance: $%.2f\n", amount, account.balance)
}

func (account *BankAccount) Withdraw(amount float64) error {
    if amount <= 0 { return errors.New("invalid withdrawal amount. Please enter a positive value.") } if amount > account.balance {
        return errors.New("insufficient funds.")
    }
    account.balance -= amount
    fmt.Printf("Successfully withdrew $%.2f. New balance: $%.2f\n", amount, account.balance)
    return nil
}

func (account *BankAccount) CheckBalance() {
    fmt.Printf("Current balance: $%.2f\n", account.balance)
}

func main() {
    account := &BankAccount{balance: 0.0}
    
    var choice int
    for {
        fmt.Println("\nWelcome to the Bank Account Simulator")
        fmt.Println("1. Deposit Money")
        fmt.Println("2. Withdraw Money")
        fmt.Println("3. Check Balance")
        fmt.Println("4. Exit")
        fmt.Print("Please enter your choice: ")
        fmt.Scanln(&choice)

        switch choice {
        case 1:
            var depositAmount float64
            fmt.Print("Enter amount to deposit: $")
            fmt.Scanln(&depositAmount)
            account.Deposit(depositAmount)
        case 2:
            var withdrawAmount float64
            fmt.Print("Enter amount to withdraw: $")
            fmt.Scanln(&withdrawAmount)
            err := account.Withdraw(withdrawAmount)
            if err != nil {
                fmt.Println(err)
            }
        case 3:
            account.CheckBalance()
        case 4:
            fmt.Println("Thank you for using the Bank Account Simulator. Goodbye!")
            return
        default:
            fmt.Println("Invalid choice. Please try again.")
        }
    }
}

Explanation of the Program

This program defines a BankAccount struct to represent a bank account with an associated balance. The following methods are implemented on the BankAccount struct:

  • Deposit(amount float64): This method adds the given amount to the balance if it’s positive. If the deposit amount is invalid (<= 0), it notifies the user.
  • Withdraw(amount float64): This method subtracts the given amount from the balance, but only if the account has sufficient funds. If there are not enough funds, it returns an error.
  • CheckBalance(): This method simply displays the current balance of the account.

The main() function contains a loop that continuously prompts the user for input to select one of the four options: deposit, withdraw, check balance, or exit. Based on the user’s choice, the corresponding method is called.

How to Run the Program

  1. Install Go from here if you don’t have it already.
  2. Save the code in a file called bank_account.go.
  3. Open a terminal or command prompt and navigate to the directory where the file is located.
  4. Run the command go run bank_account.go to execute the program.
  5. Interact with the program by entering the corresponding number for each option.
© 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 :)