exrates

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 8 Imported by: 1

README

exrates

Build Status GoDoc Go Report Card

Exchange rates client for the excellent Exchange Rates API. The API provides current and historical foreign exchange rates published by the European Central Bank.

Installation

go get github.com/adrg/exrates

Usage

Latest exchange rates
// Get all available exchange rates.
rates, err := exrates.Latest("USD", nil)
if err != nil {
    // Treat error.
    return
}
// Get specific exchange rates.
// rates, err := exrates.Latest("EUR", []string{"USD", "CAD"})

fmt.Printf("Exchange rates for %s on %s\n", rates.Base, rates.Date)
for currency, value := range rates.Values {
    fmt.Printf("%s: %f\n", currency, value)
}
Exchange rates on specific date
date := time.Date(2019, 3, 8, 0, 0, 0, 0, time.UTC)

// Get all available exchange rates.
rates, err := exrates.On("USD", date, nil)
if err != nil {
    // Treat error.
    return
}
// Get specific exchange rates.
// rates, err := exrates.On("EUR", date, []string{"USD", "CAD"})

fmt.Printf("Exchange rates for %s on %s\n", rates.Base, rates.Date)
for currency, value := range rates.Values {
    fmt.Printf("%s: %f\n", currency, value)
}
Exchange rates in date interval
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2019, 4, 22, 0, 0, 0, 0, time.UTC)

// Get all available exchange rates.
days, err := exrates.Between("USD", start, end, nil)
if err != nil {
    // Treat error.
    return
}
// Get specific exchange rates.
// days, err := exrates.Between("EUR", start, end, []string{"USD", "CAD"})

for _, day := range days {
    fmt.Printf("Exchange rates for %s on %s\n", day.Base, day.Date)
    for currency, value := range day.Values {
        fmt.Printf("%s: %f\n", currency, value)
    }
}

Supported currencies

EUR - Euro
USD - US dollar
JPY - Japanese yen
BGN - Bulgarian lev
CZK - Czech koruna
DKK - Danish krone
GBP - Pound sterling
HUF - Hungarian forint
PLN - Polish zloty
RON - Romanian leu
SEK - Swedish krona
CHF - Swiss franc
ISK - Icelandic krona
NOK - Norwegian krone
HRK - Croatian kuna
RUB - Russian rouble
TRY - Turkish lira
AUD - Australian dollar
BRL - Brazilian real
CAD - Canadian dollar
CNY - Chinese yuan renminbi
HKD - Hong Kong dollar
IDR - Indonesian rupiah
ILS - Israeli shekel
INR - Indian rupee
KRW - South Korean won
MXN - Mexican peso
MYR - Malaysian ringgit
NZD - New Zealand dollar
PHP - Philippine peso
SGD - Singapore dollar
THB - Thai baht
ZAR - South African rand

Contributing

Contributions in the form of pull requests, issues or just general feedback, are always welcome.
Before making a contribution please read CONTRIBUTING.md.

References

For more information see the Exchange Rates API.

License

Copyright (c) 2019 Adrian-George Bostan.
This project is licensed under the MIT license. See LICENSE for more details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rates

type Rates struct {
	Base   string             `json:"base"`
	Date   time.Time          `json:"date"`
	Values map[string]float64 `json:"values"`
}

Rates contains exchange rate values for a specific base currency on a particular date.

func Between

func Between(base string, start, end time.Time, currencies []string) ([]*Rates, error)

Between returns exchange rates per day for the selected base currency, in the specified interval. Specific exchange rates can be requested by using the currencies parameter. If currencies is nil, all available exchange rates are returned.

Example
package main

import (
	"fmt"
	"time"

	"github.com/adrg/exrates"
)

func main() {
	start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
	end := time.Date(2019, 4, 22, 0, 0, 0, 0, time.UTC)

	// Get all available exchange rates.
	days, err := exrates.Between("USD", start, end, nil)
	if err != nil {
		// Treat error.
		return
	}
	// Get specific exchange rates.
	// days, err := exrates.Between("EUR", start, end, []string{"USD", "CAD"})

	for _, day := range days {
		fmt.Printf("Exchange rates for %s on %s\n", day.Base, day.Date)
		for currency, value := range day.Values {
			fmt.Printf("%s: %f\n", currency, value)
		}
	}
}

func Latest

func Latest(base string, currencies []string) (*Rates, error)

Latest returns the latest exchange rates for the selected base currency. Specific exchange rates can be requested by using the currencies parameter. If currencies is nil, all available exchange rates are returned.

Example
package main

import (
	"fmt"

	"github.com/adrg/exrates"
)

func main() {
	// Get all available exchange rates.
	rates, err := exrates.Latest("USD", nil)
	if err != nil {
		// Treat error.
		return
	}
	// Get specific exchange rates.
	// rates, err := exrates.Latest("EUR", []string{"USD", "CAD"})

	fmt.Printf("Exchange rates for %s on %s\n", rates.Base, rates.Date)
	for currency, value := range rates.Values {
		fmt.Printf("%s: %f\n", currency, value)
	}
}

func On

func On(base string, date time.Time, currencies []string) (*Rates, error)

On returns the exchange rates available on the specified date, for the selected base currency. Specific exchange rates can be requested by using the currencies parameter. If currencies is nil, all available exchange rates are returned.

Example
package main

import (
	"fmt"
	"time"

	"github.com/adrg/exrates"
)

func main() {
	date := time.Date(2019, 3, 8, 0, 0, 0, 0, time.UTC)

	// Get all available exchange rates.
	rates, err := exrates.On("USD", date, nil)
	if err != nil {
		// Treat error.
		return
	}
	// Get specific exchange rates.
	// rates, err := exrates.On("EUR", date, []string{"USD", "CAD"})

	fmt.Printf("Exchange rates for %s on %s\n", rates.Base, rates.Date)
	for currency, value := range rates.Values {
		fmt.Printf("%s: %f\n", currency, value)
	}
}

Jump to

Keyboard shortcuts

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