๐ฐ 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:
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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add some amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- 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! ๐งฎ