exrates: github.com/adrg/exrates Index | Examples | Files

package exrates

import "github.com/adrg/exrates"

Index

Examples

Package Files

api.go rates.go

type Rates Uses

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 Uses

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.

Code:

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 Uses

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.

Code:

// 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 Uses

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.

Code:

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)
}

Package exrates imports 8 packages (graph). Updated 2020-10-19. Refresh now. Tools for package owners.