Start learning free

LESSON 193 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V5 in Go: Coupon Codes with a Map

Practice Read the full explanation

Use a map as a fast lookup table for coupon discounts.

You start with:

subtotal := 100
coupon := "SAVE20"
coupons := map[string]int{"SAVE10": 10, "SAVE20": 20, "SAVE50": 50}

Build:

Total: $80

Rules:

  • Look up coupon in coupons. If it is there, its value is the discount; if it is not (an unknown code), the discount is 0.
  • total := subtotal - discount.
  • Build the text "Total: $<total>" (e.g. "Total: $80"; an unknown coupon would give "Total: $100").
  • Store it in result, then print it with fmt.Println(result).

Use discount := coupons[coupon]: Go gives the zero value (0 for an int) when the key is missing.

Use strconv.Itoa(...) to convert each number to text before concatenating it.

subtotal := 100
coupon := "SAVE20"
coupons := map[string]int{"SAVE10": 10, "SAVE20": 20, "SAVE50": 50}

discount := coupons[coupon]
total := subtotal - discount
result := "Total: $" + strconv.Itoa(total)
fmt.Println(result)

STEP 1 OF 4
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V5 in Go, explained

    Problem overview

    The cart has a subtotal of 100, and the shopper typed a coupon, "SAVE20". The shop knows its coupons in a map[string]int, which maps each code to the amount it takes off: SAVE10 takes 10, SAVE20 takes 20 and SAVE50 takes 50.

    The program looks the code up, subtracts the discount from the subtotal and prints one line. For SAVE20 that is:

    Total: $80

    A shopper can type anything, so an unknown code must be harmless: it takes nothing off, and the line reads Total: $100.

    Approach: a map as a lookup table

    One way to handle coupons is a chain of if statements, one per code. Every new coupon then means new code. A map keeps the codes as data instead: adding a coupon is adding one entry to the map, and the lookup code never changes. A map also finds a key quickly, however many coupons it holds.

    The unknown-code case needs no special branch in Go. Reading a key that is not in a map does not fail: it gives the zero value of the map's value type. For map[string]int that value is 0, which is exactly the discount an unknown coupon should give. The lookup and the "no discount" rule are one expression.

    Step by step

    1. Look up the coupon. discount := coupons[coupon] reads the value stored under the shopper's code. A known code gives its amount; an unknown one gives 0.
    2. Compute the total. total := subtotal - discount. With a discount of 0 the total equals the subtotal.
    3. Build the text. result is "Total: $" joined to the total. total is an int, and Go does not join an int to a string with +, so strconv.Itoa(total) converts it first.
    4. Print result with fmt.Println.

    The solution in Go

    subtotal := 100
    coupon := "SAVE20"
    coupons := map[string]int{"SAVE10": 10, "SAVE20": 20, "SAVE50": 50}
    
    discount := coupons[coupon]
    total := subtotal - discount
    result := "Total: $" + strconv.Itoa(total)
    fmt.Println(result)
    

    Go details worth knowing here:

    • Missing keys give the zero value. The expression coupons["FREE"] does not panic and does not add a key. It evaluates to 0. The map is unchanged by the lookup.
    • The two-value form. When "missing" and "present with value 0" must be told apart, use v, ok := coupons[coupon]. The boolean ok is true only if the key exists. For example, in a map holding "SAVE0": 0, looking up "SAVE0" gives 0 true, and looking up "NOPE" gives 0 false. This lesson doesn't need it, because an unknown code and a zero discount lead to the same result.
    • Keys are case-sensitive. "save20" is a different key from "SAVE20", so it counts as unknown and gives no discount. To accept any case, convert the code first, for example with strings.ToUpper, which needs the strings package.
    • Map literal syntax. map[string]int{"SAVE10": 10, ...} gives the key type, then the value type, then the entries. Each entry is key: value.

    Edge cases and common mistakes

    • Unknown code. "FREE" gives Total: $100. No extra check is needed for it.
    • Wrong case. "save20" also gives Total: $100, since the lookup matches the key exactly.
    • A discount bigger than the subtotal. With a subtotal of 30 and SAVE50, the subtraction gives Total: $-20. The lesson's rules don't ask for a floor at zero, but a real shop would want one. A short if total < 0 { total = 0 } would add it.
    • Converting the number. "Total: $" + total fails to compile because the operands are a string and an int. Use strconv.Itoa.
    • Looking up in a nil map. Reading from a nil map is safe and gives the zero value, but writing to one panics. Here the map is only read.

    Testing

    • Subtotal 100 with SAVE20 prints Total: $80.
    • Subtotal 100 with SAVE10 prints Total: $90.
    • Subtotal 100 with FREE prints Total: $100.
    • Subtotal 100 with save20 prints Total: $100.
    • Subtotal 30 with SAVE50 prints Total: $-20.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free