Simple & efficient Go library for getting daily foreign exchange rates. Built-in support for 50+ currencies.

go-forex

Simple and efficient Go library for getting daily foreign exchange rates. Built-in support for ca. 50 currencies.

Also includes a simple commandline tool.

Motivations, alternatives and trade-offs are discussed in the technical design document.

Examples

The following example will automatically download (and cache) exchange rates from several central banks (see the list below) and then compute the exchange rate.

import "github.com/wowsignal-io/go-forex/forex"

rate, err := forex.LiveExchange().Convert("USD", "EUR", time.Date(2022, time.January, 4, 0, 0, 0, 0, time.UTC))
if err != nil { /* Handle errors. */ }
fmt.Printf("The conversion rate from USD to EUR on January 4, 2022 was %f\n", rate.Rate)
// Output: The conversion rate from USD to EUR on January 4, 2022 was 0.886603.

Currency combinations that are not directly published are supported by automatic intermediate conversions. For example, we can convert Papuan Kina to the Indian Rupee.

rate, err := forex.LiveExchange().Convert("PKG", "INR", time.Date(2022, time.January, 4, 0, 0, 0, 0, time.UTC))
if err != nil { /* Handle errors. */ }
fmt.Printf("The conversion rate from PKG to INR on January 4, 2022 was %f\n", rate.Rate)
// Output: The conversion rate from PKG to INR on January 4, 2022 was 21.255237.

Enabling the full trace shows us how the rate was computed. Here, for example, it was done by conversion through the Australian Dollar, using rates from the Royal Bank of Australia (RBA).

rate, err := forex.LiveExchange().Convert("PKG", "INR", time.Date(2022, time.January, 4, 0, 0, 0, 0, time.UTC), exchange.FullTrace)
if err != nil { /* Handle errors. */ }

// Passing exchange.FullTrace as the last argument means rate.Trace is now
// populated.
for i, step := range rate.Trace {
    fmt.Printf("Conversion step %d/%d: 1 %s = %f %s (source: %s)\n", i+1, len(rate.Trace), step.From, step.Rate, step.To, step.Info)
}

// Output:
// Conversion step 1/2: 1 PGK = 0.395226 AUD (source: RBA (inverse))
// Conversion step 2/2: 1 AUD = 53.780000 INR (source: RBA)

Commandline interface

A command called forex-convert is provided exposing the above API over the commandline.

To install:

go install github.com/wowsignal-io/go-forex/cmd/forex-convert@latest
# This places forex-convert in $GOBIN.

Convert some currencies:

forex-convert -from=PGK -to=INR -date=2021-03-01 -v
# Outputs:
# Conversion step 1/2: 1 PGK = 0.367985 AUD (source: RBA (inverse))
# Conversion step 2/2: 1 AUD = 56.850000 INR (source: RBA)
# Computed rate: 20.919963

Or, to get just the number:

forex-convert -from=PGK -to=INR -date=2021-03-01
# Outputs:
# 20.919963

Also supports other options, such as offline operation and search tolerances. Run forex-convert --help.

Offline operation

All above examples use the LiveExchange, which downloads and caches exchange data from the internet. For offline operation, OfflineExchange can be used as a drop-in replacement. However, fewer currencies are supported and only historical data is available. (However, it's easy to update the data used by the OfflineExchange using a cronjob or similar.)

Performance

The algorithm is breadth-first walk through the data while filtering edges sorted by time. Runtime scales linearly with the number of currencies and logarithmically with the length of historical data. On an M1 MacBook, computing and indirect exchange rate takes about 4,000 ns and requires about 7,000 bytes of storage.

Supported currencies and sources

Data are sourced from the following banks:

  • European Central Bank
  • Royal Bank of Australia
  • Bank of Canada

Data are refreshed every 12 hours (or manually) and cached locally in /tmp or similar path. See currencies.txt for a full list of supported currencies.

The computed exchange rates are for informational purposes only - they are unlikely to be the same as the rates actually offered, but the difference should be tolerable for home finance applications.

Future Work

More parsers, more currencies

We're missing some medium-size currencies, such as RMB. TODO contains some research notes on potential additional sources of data.

Owner
wowsignal.io
Adam's personal projects
wowsignal.io
Similar Resources

Simple example of creating an `LD_PRELOAD` library in Go that hooks LibC's main function.

LD_PRELOAD in Go Simple example of creating an LD_PRELOAD library in Go that hooks LibC's main function. Code hooks __libc_start_main to run before th

Nov 9, 2022

Logging library for Golang

GLO Logging library for Golang Inspired by Monolog for PHP, severity levels are identical Install go get github.com/lajosbencz/glo Severity levels Deb

Sep 26, 2022

The Simplest and worst logging library ever written

gologger A Simple Easy to use go logger library. Displays Colored log into console in any unix or windows platform. You can even store your logs in fi

Sep 26, 2022

Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs

gomol Gomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go. Gomol grew from a desire to have a structured logging libra

Sep 26, 2022

Minimalistic logging library for Go.

Minimalistic logging library for Go.

logger Minimalistic logging library for Go. Blog Post Features: Advanced output filters (package and/or level) Attributes Timers for measuring perform

Nov 16, 2022

Seelog is a native Go logging library that provides flexible asynchronous dispatching, filtering, and formatting.

Seelog Seelog is a powerful and easy-to-learn logging framework that provides functionality for flexible dispatching, filtering, and formatting log me

Jan 3, 2023

A pure Go contextual logging library with "batteries included"

Cue Overview Cue implements contextual logging with "batteries included". It has thorough test coverage and supports logging to stdout/stderr, file, s

Sep 16, 2019

Golang logging library

Golang logging library

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Dec 27, 2022

Library and program to parse and forward HAProxy logs

haminer Library and program to parse and forward HAProxy logs. Supported forwarder, Influxdb Requirements Go for building from source code git for dow

Aug 17, 2022
Golog is a logger which support tracing and other custom behaviors out of the box. Blazing fast and simple to use.

GOLOG Golog is an opinionated Go logger with simple APIs and configurable behavior. Why another logger? Golog is designed to address mainly two issues

Oct 2, 2022
A 12-factor app logger built for performance and happy development
A 12-factor app logger built for performance and happy development

logxi log XI is a structured 12-factor app logger built for speed and happy development. Simpler. Sane no-configuration defaults out of the box. Faste

Nov 27, 2022
A distributed logger micro-service built in go

Distributed Logger A distributed logger micro-service built in go using gRPC for internal communication with other micro-services and JSON for externa

Nov 6, 2021
This POC is built with the goal to collect events/logs from the host systems such as Kubernetes, Docker, VMs, etc. A buffering layer is added to buffer events from the collector
This POC is built with the goal to collect events/logs from the host systems such as Kubernetes, Docker, VMs, etc. A buffering layer is added to buffer events from the collector

What is does This POC is build with the goal to collect events/logs from the host systems such as Kubernetes, docker, VMs etc. A buffering layer is ad

Nov 11, 2022
Drop-in replacement for Go's stringer tool with support for bitflag sets.

stringer This program is a drop-in replacement for Go's commonly used stringer tool. In addition to generating String() string implementations for ind

Nov 26, 2021
Peimports - based on golang's debug/pe this package gives quick access to the ordered imports of pe files with ordinal support

This code is almost entirely derived from the Go standard library's debug/pe package. It didn't provide access to ordinal based entries in the IAT and

Jan 5, 2022
Tlog - Golang log but via telegram bot support

tlog golang log but via telegram bot support how to use tlog.LinkBot("token", "c

Nov 25, 2022
a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library

English | 中文 olog olog is a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library. Support outpu

Apr 12, 2023
Simple and blazing fast lockfree logging library for golang
Simple and blazing fast lockfree logging library for golang

glg is simple golang logging library Requirement Go 1.11 Installation go get github.com/kpango/glg Example package main import ( "net/http" "time"

Nov 28, 2022
Simple and extensible monitoring agent / library for Kubernetes: https://gravitational.com/blog/monitoring_kubernetes_satellite/

Satellite Satellite is an agent written in Go for collecting health information in a kubernetes cluster. It is both a library and an application. As a

Nov 10, 2022