gofinance

module
v1.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: MIT

README ยถ

๐Ÿ’ฐ GoFinance

A robust, type-safe Go library for financial calculations and money management. GoFinance provides comprehensive tools for handling complex financial mathematics including simple interest, compound interest, annuities, and precise monetary operations.


โœจ Features

๐Ÿ’ต Money Management
  • Precise decimal-based money handling using udecimal for accuracy
  • Multi-currency support with ISO code standards
  • Thread-safe operations with built-in mutex protection
  • Clean string representation of monetary values
๐Ÿ“Š Simple Interest Calculations
  • Calculate future and present values
  • Determine interest rates and periods
  • Support for multiple time units:
    • Days, Weeks, Months, Years
  • Comprehensive interest computation
๐Ÿ”„ Compound Interest Calculations
  • Complex interest rate calculations with flexible compounding frequencies
  • Support for multiple compounding periods:
    • Daily, Monthly, Bimonthly
    • Quarterly (two variations), Semi-annually, Annually
  • Rate conversion and period calculations
  • Advanced financial data structures
๐Ÿ“ˆ Annuities
  • Annuity calculations for financial planning
  • Support for various annuity types and scenarios

๐Ÿš€ Getting Started

Prerequisites
  • Go 1.26.1 or higher
  • Basic knowledge of financial mathematics
Installation
go get github.com/yeferson59/gofinance
Quick Example: Money Management
package main

import (
    "fmt"
    "github.com/yeferson59/gofinance/money"
)

func main() {
    // Create money with USD currency
    m, err := money.New(10000, 2, money.USD)
    if err != nil {
        panic(err)
    }

    // Get string representation
    str, err := m.String()
    if err != nil {
        panic(err)
    }

    fmt.Println(str) // Output: 100.00 USD
}
Quick Example: Simple Interest
package main

import (
    "fmt"
    "github.com/yeferson59/gofinance/finance/simpleinterest"
)

func main() {
    // Create a period of 2 years
    period := simpleinterest.NewPeriod(2, simpleinterest.Years)

    // Create simple interest calculation
    si := simpleinterest.New(
        future,
        present,
        interest,
        rateInterest,
        period,
    )

    periods, err := si.GetPeriods()
    if err != nil {
        panic(err)
    }

    fmt.Println("Periods:", periods)
}
Quick Example: Compound Interest
package main

import (
    "fmt"
    "github.com/yeferson59/gofinance/finance/compositeinterest"
)

func main() {
    // Create monthly compounding periods
    period, err := compositeinterest.NewPeriod(12, compositeinterest.Monthly)
    if err != nil {
        panic(err)
    }

    // Create interest rate with monthly compounding
    rateInterest, err := compositeinterest.NewRateInterest(
        0.08,
        compositeinterest.Monthly,
        compositeinterest.Nominal,
    )
    if err != nil {
        panic(err)
    }

    // Create compound interest calculation
    ci, err := compositeinterest.New(
        1000,    // present value
        1500,    // future value
        rateInterest,
        period,
    )
    if err != nil {
        panic(err)
    }

    fmt.Println("Compound interest:", ci)
}

๐Ÿ“ฆ Project Structure

gofinance/
โ”œโ”€โ”€ money/                          # Money and currency handling
โ”‚   โ”œโ”€โ”€ money.go                   # Core Money struct
โ”‚   โ”œโ”€โ”€ currency.go                # Currency definitions
โ”‚   โ””โ”€โ”€ root.go                    # Root package definitions
โ”‚
โ”œโ”€โ”€ finance/                        # Financial calculations
โ”‚   โ”œโ”€โ”€ simpleinterest/            # Simple interest calculations
โ”‚   โ”‚   โ”œโ”€โ”€ interest.go            # Interest computation
โ”‚   โ”‚   โ”œโ”€โ”€ future.go              # Future value calculations
โ”‚   โ”‚   โ”œโ”€โ”€ present.go             # Present value calculations
โ”‚   โ”‚   โ”œโ”€โ”€ rate_interest.go       # Interest rate calculations
โ”‚   โ”‚   โ”œโ”€โ”€ periods.go             # Period definitions
โ”‚   โ”‚   โ””โ”€โ”€ *_test.go              # Comprehensive tests
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ compositeinterest/         # Compound interest calculations
โ”‚   โ”‚   โ”œโ”€โ”€ root.go                # Core structures
โ”‚   โ”‚   โ”œโ”€โ”€ future.go              # Future value calculations
โ”‚   โ”‚   โ”œโ”€โ”€ present.go             # Present value calculations
โ”‚   โ”‚   โ”œโ”€โ”€ rate_interest.go       # Rate conversion
โ”‚   โ”‚   โ”œโ”€โ”€ rate_conversion.go     # Period conversions
โ”‚   โ”‚   โ”œโ”€โ”€ periods.go             # Compounding frequencies
โ”‚   โ”‚   โ”œโ”€โ”€ utils.go               # Utility functions
โ”‚   โ”‚   โ””โ”€โ”€ data.go                # Data structures
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ annuities/               # Annuity calculations
โ”‚       โ””โ”€โ”€ root.go                # Annuity definitions
โ”‚
โ”œโ”€โ”€ go.mod                          # Module definition
โ”œโ”€โ”€ go.sum                          # Dependencies hash
โ”œโ”€โ”€ Makefile                        # Development tasks
โ”œโ”€โ”€ .golangci.yaml                 # Linting configuration
โ””โ”€โ”€ README.md                       # This file

๐Ÿ› ๏ธ Development

Setup
# Clone the repository
git clone https://github.com/yeferson59/gofinance.git
cd gofinance

# Install dependencies
go mod download
Available Commands
# Run linting
make lint

# Run tests with coverage report
make test

# Format code and run imports
make fmt
Running Tests
go test -v ./...
Code Coverage
go test -coverpkg=./... -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

๐Ÿ“š Key Concepts

Simple Interest

Simple interest is calculated on the principal amount only. The formula is:

  • Interest = Principal ร— Rate ร— Time

Use this when interest is not compounded.

Compound Interest

Compound interest is calculated on the principal plus accumulated interest. It's more common in real-world scenarios:

  • A = P(1 + r/n)^(nt)

Where:

  • A = Final amount
  • P = Principal
  • r = Annual rate
  • n = Compounding frequency
  • t = Time in years
Annuities

An annuity is a series of equal payments made at regular intervals. Useful for loans, pensions, and investments.


๐Ÿ” Thread Safety

The Money struct uses sync.RWMutex to ensure thread-safe operations. You can safely use Money objects in concurrent applications.

var m *money.Money
var wg sync.WaitGroup

for i := 0; i < 100; i++ {
    wg.Add(1)
    go func() {
        defer wg.Done()
        str, _ := m.String() // Safe concurrent access
    }()
}
wg.Wait()

๐Ÿ“– Dependencies

  • github.com/quagmt/udecimal - Decimal arithmetic for precise monetary calculations
  • github.com/stretchr/testify - Testing utilities (dev dependency)

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is open source and available under the terms specified in the repository.


๐Ÿ‘จโ€๐Ÿ’ป Author

Yeferson Toloza


๐Ÿ“ž Support

If you have questions or issues:

  • Open an issue on GitHub
  • Check existing documentation
  • Review the test files for usage examples

๐ŸŽฏ Roadmap

  • Additional financial instruments support
  • Performance optimizations
  • Extended documentation with more examples
  • CLI tools for quick calculations
  • Web API wrapper

Happy calculating! ๐Ÿงฎ

Directories ยถ

Path Synopsis
finance
annuities
Package annuities provides functionality for annuity calculations.
Package annuities provides functionality for annuity calculations.
compositeinterest
Package compositeinterest provides functionality for compound interest calculations.
Package compositeinterest provides functionality for compound interest calculations.
simpleinterest
Package simpleinterest provides calculations for simple interest financial formulas.
Package simpleinterest provides calculations for simple interest financial formulas.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL