Welcome to the Currency Exchange App! This application allows you to easily track and convert currency exchange rates, helping you make informed decisions when dealing with different currencies.
Introduction
The Currency Exchange App is designed to help users track currency exchange rates in real-time and convert between different currencies. By fetching live data from an API, users can get accurate and up-to-date exchange rates to facilitate international transactions or simple personal conversions.
Objective
The main objective of this application is to create a simple, yet efficient tool for converting one currency into another. The app will use Go programming language to interact with real-time currency exchange data, which will be fetched from a public API. The program allows users to input the amount and the currency they want to convert from and into, providing the correct conversion based on current exchange rates.
Program Code
package main import ( "fmt" "log" "net/http" "encoding/json" ) const apiUrl = "https://api.exchangerate-api.com/v4/latest/USD" // Response structure to handle API response type ExchangeRates struct { Rates map[string]float64 `json:"rates"` Base string `json:"base"` Date string `json:"date"` } func getExchangeRates() (*ExchangeRates, error) { resp, err := http.Get(apiUrl) if err != nil { return nil, err } defer resp.Body.Close() var rates ExchangeRates if err := json.NewDecoder(resp.Body).Decode(&rates); err != nil { return nil, err } return &rates, nil } func convertCurrency(amount float64, fromCurrency string, toCurrency string, rates *ExchangeRates) float64 { fromRate, exists := rates.Rates[fromCurrency] if !exists { fmt.Println("Invalid from currency") return 0 } toRate, exists := rates.Rates[toCurrency] if !exists { fmt.Println("Invalid to currency") return 0 } return (amount / fromRate) * toRate } func main() { var amount float64 var fromCurrency, toCurrency string fmt.Println("Welcome to Currency Exchange App!") fmt.Print("Enter amount to convert: ") fmt.Scanf("%f", &amount) fmt.Print("Enter the base currency (e.g., USD): ") fmt.Scanf("%s", &fromCurrency) fmt.Print("Enter the target currency (e.g., EUR): ") fmt.Scanf("%s", &toCurrency) rates, err := getExchangeRates() if err != nil { log.Fatalf("Error fetching exchange rates: %v", err) } convertedAmount := convertCurrency(amount, fromCurrency, toCurrency, rates) fmt.Printf("Converted Amount: %.2f %s\n", convertedAmount, toCurrency) }
Explanation of Program Structure
This program is structured to allow users to input the amount they wish to convert and specify the currencies. The following key parts are crucial:
- getExchangeRates: A function that fetches exchange rates from a live API (ExchangeRate-API). It returns a structured response containing the currency rates.
- convertCurrency: A function that performs the conversion of currencies based on the rates fetched. It calculates the conversion by dividing the amount by the base currency’s rate and then multiplying it by the target currency’s rate.
- main: The entry point of the program. It gathers user input, calls the necessary functions, and prints out the converted amount.
How to Run the Program
To run this program:
- Ensure you have Go installed on your system. If not, download and install Go from the official website: https://golang.org/dl/.
- Create a new Go file (e.g., currency_converter.go) and paste the provided code into it.
- In your terminal, navigate to the folder where your Go file is located.
- Run the command go run currency_converter.go to execute the program.
- Follow the prompts to enter the amount and currencies for conversion.