Skip to content

ariankhani/calculator-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Go Calculator

A small, idiomatic Go calculator designed for teaching and quick demos. The project includes a focused calc package with pure functions, unit tests, and a tiny CLI that supports flags, positional arguments, and an interactive REPL.

Files

  • calc/ : calculator package implementing arithmetic and factorial.
  • cmd/calc : command-line program and interactive REPL.

Supported operations

  • add, sub, mul, div, pow, fact

Requirements

  • Go 1.20+ installed and available on your PATH.

Quick start

  1. From the project root, run tests:
cd /home/arian/project/golang
go test ./...
  1. Run the CLI (non-interactive examples):
# flags
go run ./cmd/calc -op add -a 2 -b 3
go run ./cmd/calc -op div -a 10 -b 2

# positional
go run ./cmd/calc add 2 3
  1. Start the interactive REPL (recommended for demos):
go run ./cmd/calc

# Example interactive session (you can type these lines when prompted):
# calc> + 2 3      => prints 5
# calc> pow 2 10   => prints 1024
# calc> fact 5     => prints 120
# calc> quit       => exit

REPL features

  • Prompt: calc>
  • Accepts single-line commands: add 2 3, + 2 3, pow 2 10, fact 5.
  • If you enter only an operation (e.g. +), the REPL will prompt for the operands.
  • Commands: help, quit, exit, q.

How it works (brief)

  • calc package (calc/calc.go): small pure functions returning values or errors where appropriate.
    • Add/Sub/Mul/Pow return float64 results.
    • Div(a,b) (float64, error) returns an error for division by zero.
    • Factorial(n uint64) (uint64, error) returns an error when n > 20 to avoid uint64 overflow.
  • cmd/calc/main.go: CLI and REPL. It supports three modes:
    1. Flag mode: -op, -a, -b, -n for scripting.
    2. Positional mode: go run ./cmd/calc add 2 3.
    3. REPL mode: go run ./cmd/calc for interactive use.

Design notes (for a short presentation)

  • Small package surface makes code easy to read and test.
  • Explicit error returns (Go idiom) — no panics for normal input errors.
  • Tests live next to the package in calc/calc_test.go and cover happy-path and error cases.

Presentation tips

  • Show Div and explain explicit error handling for divide-by-zero.
  • Show Factorial and explain the overflow guard (n <= 20) and why big.Int would be used for larger factorials.
  • Run go test ./... live to demonstrate automated correctness.

Extending the project

  • Add big.Int factorial if you need arbitrarily large results.
  • Add an expression parser or REPL history for a richer demo.

If you want, I can also add a short scripts/demo.sh that runs a few example commands and prints outputs for slide screenshots.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages