Skip to content

Exercise 7 of 10 · Maps

Currency Converter

What you will make

A program that converts a fixed USD amount into euros, pounds, and yen using a map of fixed exchange rates.

The one new idea: Maps: map[string]float64 literals, looking values up by key (including the zero-value gotcha for a missing key), and formatting floats with %.2f

Maps are how Go programs look things up by name instead of position, and a mistyped key is one of the sneakiest bugs around because Go doesn't complain — it just hands back a zero.

Go straight to the code ↓

A Go map like map[string]float64{...} associates keys with values, and you read one back with rates["EUR"]. The convenient part is also the dangerous part: looking up a key that was never set doesn't panic or error, it just returns the zero value for the map's value type — 0.0 for a float64 map. That means a typo in a map key can produce a number that looks plausible (zero times anything is still zero, but zero itself can slip past a quick glance) instead of an obvious crash.

Worked example

Here's the same rates map converting a different amount:

Go
package main

import "fmt"

func main() {
	rates := map[string]float64{
		"EUR": 0.92,
		"GBP": 0.79,
		"JPY": 149.50,
	}

	amountUSD := 20.0

	fmt.Println("Currency conversion from $20.00:")
	fmt.Printf("EUR: %.2f\n", amountUSD*rates["EUR"])
	fmt.Printf("GBP: %.2f\n", amountUSD*rates["GBP"])
	fmt.Printf("JPY: %.2f\n", amountUSD*rates["JPY"])
}
Output
Currency conversion from $20.00:
EUR: 18.40
GBP: 15.80
JPY: 2990.00

Each line multiplies the dollar amount by the correct rate looked up under its correct three-letter key.

Your turn

The starter program converts $50 into three currencies, but one line looks up the rate under a misspelled key, and another line adds the rate instead of multiplying by it. Fix both marked lines so all three conversions come out correct.

If something goes wrong

If one of your converted amounts comes out as exactly 0.00, suspect a mistyped map key first — that's the classic silent-zero symptom. If a converted amount looks wildly too small compared to the others (like being close to just the rate itself, barely bigger than 50), check whether that line is adding the rate instead of multiplying by it.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Typing a map key slightly wrong, like GPB instead of GBP.
Go maps don't raise an error for a missing key — reading one just returns the zero value for the map's value type (0.0 for float64), which is easy to mistake for a real result.
Adding a rate to an amount instead of multiplying by it.
An exchange rate is a multiplier — amountUSD * rate scales the amount, while amountUSD + rate just adds two unrelated numbers together.
Forgetting the .2 in %.2f.
Plain %f prints many decimal places by default, which doesn't match a currency amount's usual two-decimal formatting.
Assuming map iteration order matches the order keys were written.
This program looks values up directly by key rather than looping over the map, but it's worth knowing that ranging over a Go map does not guarantee any particular order.

Want a blank editor instead? Open the Go playground.