Golang

 

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:

  1. Ensure you have Go installed on your system. If not, download and install Go from the official website: https://golang.org/dl/.
  2. Create a new Go file (e.g., currency_converter.go) and paste the provided code into it.
  3. In your terminal, navigate to the folder where your Go file is located.
  4. Run the command go run currency_converter.go to execute the program.
  5. Follow the prompts to enter the amount and currencies for conversion.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)