π Learn Any Programming Language Faster β by Building Simple, Practical Projects! π§ π»
Today, I built a simple USSD-based Bikash (mobile banking) console app in Go (Golang).
π This basic app includes:
β Variables and data types
π Loops and conditional logic
π₯ User input handling
π€ Console output
π¦ Function modularity
π Step-by-Step: How I Developed My Simple Bikash App in Go
β
Started with basic functionality: Display welcome message and show a simple menu.
π§ Used if-else statements: Handled user choices like checking balance, cash in, and cash out.
βοΈ Added validation: Ensured users canβt cash in or out with invalid amounts (e.g., negative or zero).
π Introduced a loop: Used a for loop to keep the app running without restarting it.
π Added exit condition: Allowed users to exit the app gracefully using a menu option.
π Replaced if-else with switch: Made the code cleaner and easier to manage.
π² Run:
-
cd BikashApp
-
go run bikash.go
I want to make a simple bikash app which we see in our real life
by typing *247#
.
π² Final full code
// Run: go run bikash.go
package main
import "fmt"
func main() {
accountBalance := 1000.00
// Ask user to dial *247# before starting
var ussdInput string
fmt.Print("Please dial *247# to start your transaction: ")
fmt.Scan(&ussdInput)
if ussdInput != "*247#" {
fmt.Println("Invalid USSD code. Exiting...")
return
}
fmt.Println("Welcome to Fake-Bikash App")
fmt.Println("-------------------------")
for {
fmt.Println("What do you want to do?")
fmt.Println("1. Check Your Balance")
fmt.Println("2. Cash In")
fmt.Println("3. Cash Out")
fmt.Println("4. Exit Fake-Bikash App")
var choice int
fmt.Print("Enter your option: ")
fmt.Scan(&choice)
// fmt.Println("You have selected option:", choice)
switch choice {
case 1:
fmt.Printf("You Balance is : %.2f Tk\n", accountBalance)
case 2:
//user cash in
fmt.Print("Enter your Cash in amount: ")
var cashInAmount float64
fmt.Scan(&cashInAmount)
fmt.Printf("You entered: %.2f Tk\n", cashInAmount)
if cashInAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
// return;
continue
}
accountBalance = accountBalance + cashInAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
case 3:
//user cash out
fmt.Print(("Enter your Cash out amount: "))
var cashOutAmount float64
fmt.Scan((&cashOutAmount))
if cashOutAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
continue
}
if cashOutAmount > accountBalance {
fmt.Println("Insufficient balance!")
// return
continue
}
accountBalance = accountBalance - cashOutAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
default:
fmt.Println("-----Thanks for chosing Fake-Bikash App----")
return
}
}
}
output:
β― go run bikash.go
Please dial *247# to start your transaction: 77878
Invalid USSD code. Exiting...
β― clear
β― go run bikash.go
Please dial *247# to start your transaction: *247#
Welcome to Fake-Bikash App
-------------------------
What do you want to do?
1. Check Your Balance
2. Cash In
3. Cash Out
4. Exit Fake-Bikash App
Enter your option: 1
You Balance is : 1000.00 Tk
What do you want to do?
1. Check Your Balance
2. Cash In
3. Cash Out
4. Exit Fake-Bikash App
Enter your option: 2
Enter your Cash in amount: 200
You entered: 200.00 Tk
Balanced Updated!Current balance is: 1200.00 Tk
What do you want to do?
1. Check Your Balance
2. Cash In
3. Cash Out
4. Exit Fake-Bikash App
Enter your option: 1
You Balance is : 1200.00 Tk
What do you want to do?
1. Check Your Balance
2. Cash In
3. Cash Out
4. Exit Fake-Bikash App
Enter your option: 3
Enter your Cash out amount: -90
Invalid amount! Please enter a amount greater than 0.
What do you want to do?
1. Check Your Balance
2. Cash In
3. Cash Out
4. Exit Fake-Bikash App
Enter your option: 3
Enter your Cash out amount: 1100
Balanced Updated!Current balance is: 100.00 Tk
What do you want to do?
1. Check Your Balance
2. Cash In
3. Cash Out
4. Exit Fake-Bikash App
Enter your option: 1
You Balance is : 100.00 Tk
What do you want to do?
1. Check Your Balance
2. Cash In
3. Cash Out
4. Exit Fake-Bikash App
Enter your option: 4
-----Thanks for chosing Fake-Bikash App----
- Basic task
package main
import "fmt"
func main(){
fmt.Println("Welcome to Bikash App");
fmt.Println("What do you want to do?");
fmt.Println("1. Check Your Balance");
fmt.Println("2. Cash In");
fmt.Println("3. Cash Out");
fmt.Println("4. Exit Bikash App");
}
- Next I implemnt if-else based on user choice.
//Run: go run bikash.go
package main
import "fmt"
func main() {
accountBalance :=1000.00;
fmt.Println("Welcome to Fake-Bikash App")
fmt.Println("-------------------------")
fmt.Println("What do you want to do?")
fmt.Println("1. Check Your Balance")
fmt.Println("2. Cash In")
fmt.Println("3. Cash Out")
fmt.Println("4. Exit Fake-Bikash App")
var choice int
fmt.Print("Enter your option: ")
fmt.Scan(&choice)
// fmt.Println("You have selected option:", choice)
if choice == 1 {
fmt.Printf("You Balance is : %.2f Tk\n", accountBalance);
}else if choice == 2 {
//user cash in
fmt.Print("Enter your Cash in amount: ");
var cashInAmount float64;
fmt.Scan(&cashInAmount,)
fmt.Printf("You entered: %.2f Tk\n", cashInAmount)
accountBalance=accountBalance+cashInAmount;
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance);
}else if choice==3 {
fmt.Print(("Enter your Cash out amount: "))
var cashOutAmount float64;
fmt.Scan((&cashOutAmount));
accountBalance=accountBalance-cashOutAmount;
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance);
}else{
fmt.Println("Exiting Fake-Bikash App")
}
}
- Add Vaditation for cash in and cash out part
// Run: go run bikash.go
package main
import "fmt"
func main() {
accountBalance := 1000.00
fmt.Println("Welcome to Fake-Bikash App")
fmt.Println("-------------------------")
fmt.Println("What do you want to do?")
fmt.Println("1. Check Your Balance")
fmt.Println("2. Cash In")
fmt.Println("3. Cash Out")
fmt.Println("4. Exit Fake-Bikash App")
var choice int
fmt.Print("Enter your option: ")
fmt.Scan(&choice)
// fmt.Println("You have selected option:", choice)
if choice == 1 {
fmt.Printf("You Balance is : %.2f Tk\n", accountBalance)
} else if choice == 2 {
//user cash in
fmt.Print("Enter your Cash in amount: ")
var cashInAmount float64
fmt.Scan(&cashInAmount)
fmt.Printf("You entered: %.2f Tk\n", cashInAmount)
if cashInAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
return
}
accountBalance = accountBalance + cashInAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
} else if choice == 3 {
fmt.Print(("Enter your Cash out amount: "))
var cashOutAmount float64
fmt.Scan((&cashOutAmount));
if cashOutAmount <=0{
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
};
if cashOutAmount > accountBalance {
fmt.Println("Insufficient balance!")
return
}
accountBalance = accountBalance - cashOutAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
} else {
fmt.Println("Exiting Fake-Bikash App")
}
}
- Add for loop so that i can selete option as much as i need
// Run: go run bikash.go
package main
import "fmt"
func main() {
accountBalance := 1000.00
for i := 0; i < 5; i++ {
fmt.Println("Welcome to Fake-Bikash App")
fmt.Println("-------------------------")
fmt.Println("What do you want to do?")
fmt.Println("1. Check Your Balance")
fmt.Println("2. Cash In")
fmt.Println("3. Cash Out")
fmt.Println("4. Exit Fake-Bikash App")
var choice int
fmt.Print("Enter your option: ")
fmt.Scan(&choice)
// fmt.Println("You have selected option:", choice)
if choice == 1 {
fmt.Printf("You Balance is : %.2f Tk\n", accountBalance)
} else if choice == 2 {
//user cash in
fmt.Print("Enter your Cash in amount: ")
var cashInAmount float64
fmt.Scan(&cashInAmount)
fmt.Printf("You entered: %.2f Tk\n", cashInAmount)
if cashInAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
return
}
accountBalance = accountBalance + cashInAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
} else if choice == 3 {
fmt.Print(("Enter your Cash out amount: "))
var cashOutAmount float64
fmt.Scan((&cashOutAmount))
if cashOutAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
}
if cashOutAmount > accountBalance {
fmt.Println("Insufficient balance!")
return
}
accountBalance = accountBalance - cashOutAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
} else {
fmt.Println("Exiting Fake-Bikash App")
}
}
}
- Add infinite time to run the loop and add condition for loop break
// Run: go run bikash.go
package main
import "fmt"
func main() {
accountBalance := 1000.00;
fmt.Println("Welcome to Fake-Bikash App")
fmt.Println("-------------------------")
for {
fmt.Println("What do you want to do?")
fmt.Println("1. Check Your Balance")
fmt.Println("2. Cash In")
fmt.Println("3. Cash Out")
fmt.Println("4. Exit Fake-Bikash App")
var choice int
fmt.Print("Enter your option: ")
fmt.Scan(&choice)
// fmt.Println("You have selected option:", choice)
if choice == 1 {
fmt.Printf("You Balance is : %.2f Tk\n", accountBalance)
} else if choice == 2 {
//user cash in
fmt.Print("Enter your Cash in amount: ")
var cashInAmount float64
fmt.Scan(&cashInAmount)
fmt.Printf("You entered: %.2f Tk\n", cashInAmount)
if cashInAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
// return;
continue
}
accountBalance = accountBalance + cashInAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
} else if choice == 3 {
fmt.Print(("Enter your Cash out amount: "))
var cashOutAmount float64
fmt.Scan((&cashOutAmount))
if cashOutAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
}
if cashOutAmount > accountBalance {
fmt.Println("Insufficient balance!")
return
}
accountBalance = accountBalance - cashOutAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
} else {
fmt.Println("-----Thanks for chosing Fake-Bikash App----")
break;
}
}
}
- Add switch case
// Run: go run bikash.go
package main
import "fmt"
func main() {
accountBalance := 1000.00
fmt.Println("Welcome to Fake-Bikash App")
fmt.Println("-------------------------")
for {
fmt.Println("What do you want to do?")
fmt.Println("1. Check Your Balance")
fmt.Println("2. Cash In")
fmt.Println("3. Cash Out")
fmt.Println("4. Exit Fake-Bikash App")
var choice int
fmt.Print("Enter your option: ")
fmt.Scan(&choice)
// fmt.Println("You have selected option:", choice)
switch choice {
case 1:
fmt.Printf("You Balance is : %.2f Tk\n", accountBalance)
case 2:
//user cash in
fmt.Print("Enter your Cash in amount: ")
var cashInAmount float64
fmt.Scan(&cashInAmount)
fmt.Printf("You entered: %.2f Tk\n", cashInAmount)
if cashInAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
// return;
continue
}
accountBalance = accountBalance + cashInAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
case 3:
//user cash out
fmt.Print(("Enter your Cash out amount: "))
var cashOutAmount float64
fmt.Scan((&cashOutAmount))
if cashOutAmount <= 0 {
fmt.Println("Invalid amount! Please enter a amount greater than 0.")
continue
}
if cashOutAmount > accountBalance {
fmt.Println("Insufficient balance!")
// return
continue
}
accountBalance = accountBalance - cashOutAmount
fmt.Printf("Balanced Updated!Current balance is: %.2f Tk\n", accountBalance)
default:
fmt.Println("-----Thanks for chosing Fake-Bikash App----")
return
}
}
}
## Optimized Final version Code
go
package main
import "fmt"
var accountBalance float64 =1000.00
func main(){
// var ussdInput string
// fmt.Print("Please dial *247# to start your transaction: ")
// fmt.Scan(&ussdInput)
// if ussdInput != "*247#" {
// fmt.Println("Invalid USSD code. Exiting...")
// return
// }
fmt.Println("Welcome to Fake-Bikash App")
fmt.Println("-------------------------")
for {
showMenu()
var choice int
fmt.Scan(&choice)
switch choice {
case 1:
checkBalance()
case 2:
cashIn()
case 3:
cashOut()
case 4:
fmt.Println("-----Thanks for choosing Fake-Bikash App----")
return
default:
fmt.Println("Invalid choice. Please try again.")
}
}
}
func showMenu(){
fmt.Println("What do you want to do?")
fmt.Println("1. Check Your Balance")
fmt.Println("2. Cash In")
fmt.Println("3. Cash Out")
fmt.Println("4. Exit Fake-Bikash App")
}
func checkBalance(){
fmt.Printf("Your Balance is: %.2f Tk\n", accountBalance)
}
func cashIn() {
var amount float64
fmt.Print("Enter your Cash In amount: ")
fmt.Scan(&amount)
if !isValidAmount(amount) {
return
}
accountBalance += amount
fmt.Printf("Balance Updated! Current balance is: %.2f Tk\n", accountBalance)
}
func cashOut() {
var amount float64
fmt.Print("Enter your Cash Out amount: ")
fmt.Scan(&amount)
if !isValidAmount(amount) {
return
}
if amount > accountBalance {
fmt.Println("Insufficient balance!")
return
}
accountBalance -= amount
fmt.Printf("Balance Updated! Current balance is: %.2f Tk\n", accountBalance)
}
func isValidAmount(amount float64) bool {
if amount <= 0 {
fmt.Println("Invalid amount! Please enter an amount greater than 0.")
return false
}
return true
}
Top comments (0)