Service is dead, see Formicka/exchangerate.host#245
Go library for current & historical exchange rates, forex & cryptocurrency conversion, fluctuation, and timeseries using the new Free foreign exchange rates API by arzzen (GitHub)
- Currency conversion, historical & current exchange rates, timeseries and fluctuations.
- No authentication/token needed!
- Select any base currency.
- 171 forex currency and 6000+ cryptocurrency!
- No dependencies, only standard library.
- Easy to use.
Upgrading from asvvvad/exchange to version 1.0.0 (for further upgrading please view CHANGELOG.md):
- no built-in caching, do it yourself
- changed return types from
big.Float
tofloat64
and the second argument's type forConvertTo()
fromint
tofloat64
package main
import (
"fmt"
"github.com/rehhouari/exchange"
)
func main() {
// Create a new Exchange instance and set USD as the base currency for the exchange rates and conversion
ex := exchange.New("USD")
// convert 10 USD to EUR
fmt.Println(ex.ConvertTo("EUR", 10.0))
// You can convert between 171 fiat and +6000 cryptocurrency as well!
// convert 10 USD to BTC
fmt.Println(ex.ConvertTo("BTC", 10.0))
// convert 10 USD to EUR at 2012-12-12 (date must be in the format YYYY-MM-DD)
fmt.Println(ex.ConvertAt("2012-12-12", "EUR", 10.0))
// Get the available forex/fiat codes ([]string)
forexCodes, _ := ex.ForexCodes()
// Get the available crypto codes ([]string)
// Warning: +6000
cryptoCodes, _ := ex.CryptoCodes()
fmt.Println(cryptoCodes)
// Get the forex codes data, includes code and description.
forexData, _ := ex.ForexData()
// Get the crypto codes data, includes code and description.
// Warning: +6000
cryptoData, _ := ex.CryptoData()
fmt.Println(cryptoData["BTC"]["code"])
// loop through the forex codes
for _, code := range forexCodes {
// print the forex codes data in the format: USD: US Dollar
fmt.Println(code+":", forexData[code]["description"])
}
// Change the base currency to euro
ex.SetBase("EUR")
// Get the latest exchange rates with all currencies (Base is EUR)
// exchangerate.host update them at midnight GMT so make sure to cache it till next day
fmt.Println(ex.LatestRatesAll())
// Get the latest rates with multiple currencies, not all (USD and JPY only)
fmt.Println(ex.LatestRatesMultiple([]string{"USD", "JPY"}))
// Get the exchange rates at 2012-12-12 but only with USD
fmt.Println(ex.HistoricalRatesSingle("2012-12-12", "USD"))
// Get historical rates between 2012 12 10 and 2012 12 12 for JPY and GBP
fmt.Println(ex.TimeseriesMultiple("2012-12-10", "2012-12-12", []string{"USD", "JPY"}))
// Get the fluctuation between 2012 12 10 and 2012 12 12 with USD
fluctuation, _ := ex.FluctuationSingle("2012-12-10", "2012-12-12", "USD")
// Print the change
fmt.Println(fluctuation["change"])
}
-
ConvertTo, ConvertAt, HistoricalRatesSingle, LatestRatesSingle
-
float64
, error
-
LatestRatesAll, LatestRatesMultiple, HistoricalRatesAll, HistoricalRatesMultiple:
-
map[symbol(string)]rate(float64)
-
ForexCodes
-
[]string{codes}
, error
-
ForexData
-
map[symbol]map[ code description ]string
, error
-
CryptoCodes
-
[]string{codes}
, error
-
CryptoData
-
map[symbol]map[ symbol name ]string
, error
-
FluctuationAll, FluctuationMultiple,
-
map[symbol]map[ start_rate end_rate change change_pct ]float64
, error
-
FluctuationSingle
-
map[ start_rate end_rate change change_pct ]float64
, error
-
TimeseriesAll, TimeseriesMultiple
-
map[date]map[symbols]float64
, error
-
TimeseriesSingle
-
map[date]map[symbol]float64
, error
- Exchange rates are refreshed every midnight GMT, cache results when using!
- You can use All, Multiple, Single with all of LatestRates, HistoricalRates, Timeseries and Fluctuation.
- Oldest date for historical rates and conversion is 1999-01-04
- Maximum allowed timeframe for Timeseries is 365 days
- Use decimal to handle
float64
results withdecimal.NewFromFloat
.
This is my first Go library and I had trouble with JSON parsing
(and I still do, didn't use bitly/simplejson
to reduce dependencies).
There's a lot of room for improvement.