ecbrates

package module
v0.0.0-...-f378209 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2016 License: MIT Imports: 5 Imported by: 5

README

The European Central Bank exchange rates

A package to get the ECB exchange rates for use with Go (golang) services

Build Status GoDoc

Examples

Check a current exchange rate

package main

import (
	"fmt"
	"log"

	"github.com/openprovider/ecbrates"
)

func main() {
	r, err := ecbrates.New()
	if err != nil {
		log.Fatal("Error: ", err)
	}

	// Case 1: get dollar rate relative to euro
	if value, ok := r.Rate[ecbrates.USD].(string); ok {
		fmt.Println("Exchange rate", r.Date, ": EUR 1 -> USD", value)
	}

	// Case 2: convert of 100 euros to dollars
	if value, err := r.Convert(100, ecbrates.EUR, ecbrates.USD); err == nil {
		fmt.Println("Exchange rate", r.Date, ": EUR 100.0 -> USD", value)
	}

	// Case 3: convert of 100 dollars to yens
	if value, err := r.Convert(100, ecbrates.USD, ecbrates.JPY); err == nil {
		fmt.Println("Exchange rate", r.Date, ": USD 100.0 -> JPY", value)
	}
}

Show history of exchange rates for EUR -> USD

package main

import (
	"fmt"
	"log"

	"github.com/openprovider/ecbrates"
)

func main() {
	rates, err := ecbrates.Load() // 90 days history
	// rates, err := ecbrates.LoadAll() // ALL history
	if err != nil {
		log.Fatal("Error: ", err)
	}

	for _, r := range rates {
		if value, ok := r.Rate[ecbrates.USD].(string); ok {
			fmt.Println("Exchange rate", r.Date, ": EUR 1 -> USD", value)
		}
	}
}

Contributors (unsorted)

All the contributors are welcome. If you would like to be the contributor please accept some rules.

  • The pull requests will be accepted only in "develop" branch
  • All modifications or additions should be tested

Thank you for your understanding!

License

MIT Public License

Documentation

Overview

Package ecbrates 0.3.0 This package helps parse the ECB exchange rates and use it for an applications

Example 1:

package main

import (
	"fmt"
	"log"

	"github.com/openprovider/ecbrates"
)

func main() {
	r, err := ecbrates.New()
	if err != nil {
		log.Fatal("Error: ", err)
	}

	// Case 1: get dollar rate relative to euro
	if value, ok := r.Rate[ecbrates.USD].(string); ok {
		fmt.Println("Exchange rate", r.Date, ": EUR 1 -> USD", value)
	}

	// Case 2: convert of 100 euros to dollars
	if value, err := r.Convert(100, ecbrates.EUR, ecbrates.USD); err == nil {
		fmt.Println("Exchange rate", r.Date, ": EUR 100.0 -> USD", value)
	}
}

Example 2:

package main

import (
	"fmt"
	"log"

	"github.com/openprovider/ecbrates"
)

func main() {
	rates, err := ecbrates.Load() // load last 90 days
	// rates, err := ecbrates.LoadAll() // <- load ALL historical data, lots of data!
	if err != nil {
		log.Fatal("Error: ", err)
	}

	// Show history of exchange rates for EUR -> USD
	for _, r := range rates {
		if value, ok := r.Rate[ecbrates.USD].(string); ok {
			fmt.Println("Exchange rate", r.Date, ": EUR 1 -> USD", value)
		}
	}
}

The European Central Bank exchange rates

Index

Constants

This section is empty.

Variables

View Source
var Currencies = []Currency{
	AUD, BGN, BRL, CAD, CHF, CNY, CZK, DKK, EUR, GBP, HKD,
	HRK, HUF, IDR, ILS, INR, JPY, KRW, LTL, MXN, MYR, NOK,
	NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, TRY, USD, ZAR,

	CYP, EEK, ISK, LVL, MTL, SIT, SKK, ROL, TRL,
}

Currencies are valid values for currency

Functions

This section is empty.

Types

type Currency

type Currency string

Currency type as a link to string

const (
	AUD Currency = "AUD" // Australian Dollar (A$)
	BGN Currency = "BGN" // Bulgarian Lev (BGN)
	BRL Currency = "BRL" // Brazilian Real (R$)
	CAD Currency = "CAD" // Canadian Dollar (CA$)
	CHF Currency = "CHF" // Swiss Franc (CHF)
	CNY Currency = "CNY" // Chinese Yuan (CN¥)
	CZK Currency = "CZK" // Czech Republic Koruna (CZK)
	DKK Currency = "DKK" // Danish Krone (DKK)
	EUR Currency = "EUR" // Euro (€)
	GBP Currency = "GBP" // British Pound Sterling (£)
	HKD Currency = "HKD" // Hong Kong Dollar (HK$)
	HRK Currency = "HRK" // Croatian Kuna (HRK)
	HUF Currency = "HUF" // Hungarian Forint (HUF)
	IDR Currency = "IDR" // Indonesian Rupiah (IDR)
	ILS Currency = "ILS" // Israeli New Sheqel (₪)
	INR Currency = "INR" // Indian Rupee (Rs.)
	JPY Currency = "JPY" // Japanese Yen (¥)
	KRW Currency = "KRW" // South Korean Won (₩)
	LTL Currency = "LTL" // Lithuanian Litas (LTL)
	MXN Currency = "MXN" // Mexican Peso (MX$)
	MYR Currency = "MYR" // Malaysian Ringgit (MYR)
	NOK Currency = "NOK" // Norwegian Krone (NOK)
	NZD Currency = "NZD" // New Zealand Dollar (NZ$)
	PHP Currency = "PHP" // Philippine Peso (Php)
	PLN Currency = "PLN" // Polish Zloty (PLN)
	RON Currency = "RON" // Romanian Leu (RON)
	RUB Currency = "RUB" // Russian Ruble (RUB)
	SEK Currency = "SEK" // Swedish Krona (SEK)
	SGD Currency = "SGD" // Singapore Dollar (SGD)
	THB Currency = "THB" // Thai Baht (฿)
	TRY Currency = "TRY" // Turkish Lira (TRY)
	USD Currency = "USD" // US Dollar ($)
	ZAR Currency = "ZAR" // South African Rand (ZAR)

	// Historical currencies
	CYP Currency = "CYP"
	EEK Currency = "EEK"
	ISK Currency = "ISK"
	LVL Currency = "LVL"
	MTL Currency = "MTL"
	SIT Currency = "SIT"
	SKK Currency = "SKK"
	ROL Currency = "ROL"
	TRL Currency = "TRL"
)

List of all supported currencies

func (Currency) IsValid

func (c Currency) IsValid() bool

IsValid check Currency for valid value

type Rates

type Rates struct {
	Date string
	Rate map[Currency]interface{}
}

Rates represent date and currency exchange rates

func Load

func Load() ([]Rates, error)

Load - create a new instances of the rates and fetch data for the last 90 days from ECB

func LoadAll

func LoadAll() ([]Rates, error)

LoadAll - create a new instances of the rates and fetch all historical data from ECB

func New

func New() (*Rates, error)

New - create a new instance of the rates and fetch a data from ECB

func (*Rates) Convert

func (r *Rates) Convert(value float64, from, to Currency) (float64, error)

Convert a value "from" one Currency -> "to" other Currency

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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