How to get live (spot) forex rate using API in Go?

< Back to Guides

Go is a statically typed, compiled programming language. This tutorial is how to get live forex rate using Go programming language.

Instructions

  1. Get a free API key to use by signing up at forexrateapi.com and replace REPLACE_ME with your API key.
  2. Update base value to a currency or remove this query param.
  3. Update currencies value to a list of values or remove this query param. Below is an example usage for foreign exchange conversion API in use.

For 2 and 3, you can find this documented at Offical Documentation

 

Fetch live forex rate in Go

package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "https://api.forexrateapi.com/v1/latest?api_key=REPLACE_ME&base=USD&currencies=EUR,JPY,CAD"
  method := "GET"

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, nil)

  if err != nil {
    fmt.Println(err)
    return
  }
  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}