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
- 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. - Compute the total.
total := subtotal - discount. With a discount of 0 the total equals the subtotal. - Build the text.
resultis"Total: $"joined to the total.totalis anint, and Go does not join anintto a string with+, sostrconv.Itoa(total)converts it first. - Print
resultwithfmt.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 booleanokis true only if the key exists. For example, in a map holding"SAVE0": 0, looking up"SAVE0"gives0 true, and looking up"NOPE"gives0 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 withstrings.ToUpper, which needs thestringspackage. - Map literal syntax.
map[string]int{"SAVE10": 10, ...}gives the key type, then the value type, then the entries. Each entry iskey: value.
Edge cases and common mistakes
- Unknown code.
"FREE"givesTotal: $100. No extra check is needed for it. - Wrong case.
"save20"also givesTotal: $100, since the lookup matches the key exactly. - A discount bigger than the subtotal. With a subtotal of 30 and
SAVE50, the subtraction givesTotal: $-20. The lesson's rules don't ask for a floor at zero, but a real shop would want one. A shortif total < 0 { total = 0 }would add it. - Converting the number.
"Total: $" + totalfails to compile because the operands are a string and anint. Usestrconv.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
SAVE20printsTotal: $80. - Subtotal 100 with
SAVE10printsTotal: $90. - Subtotal 100 with
FREEprintsTotal: $100. - Subtotal 100 with
save20printsTotal: $100. - Subtotal 30 with
SAVE50printsTotal: $-20.
