Problem overview
Version 1 computed a bonus for one employee. Version 2 has two candidates, stored as name1, productivity1, salary1 and name2, productivity2, salary2. The more productive employee wins. The program prints the winner's name and then their bonus, 15% of the salary rounded down.
If the productivities are equal, employee 1, the one listed first, wins. With the starting data, Mary Jane (133) beats John Doe (83), and her bonus is 335000 * 15 / 100 = 50250:
Mary Jane
Bonus: $50250
Approach: decide first, then compute once
There are two ways to structure this. You could write the bonus calculation and the printing in both branches of an if. That duplicates code, and each later change has to be made twice. The lesson uses a cleaner pattern: the if/else only decides who won and records the winner's name and salary in two variables. Everything after it, the bonus and the printing, is written once and works on those two variables.
The pattern scales: another branch could be added and nothing below the chain would change.
The two variables also carry over from version 1: winnerSalary takes the place of salary in the bonus formula, and the printing lines stay the same as before.
Go has no ternary operator (condition ? a : b), so a statement-level if/else is the way to choose between two values. You will often see this shape in Go code: declare the variables, then fill them in on each path.
Traced on the starting data: 83 >= 133 is false, so the else branch runs, winnerName becomes "Mary Jane" and winnerSalary becomes 335000. The line after the branches then computes 335000 * 15 / 100, which is 50250.
Step by step
- Declare the winner's variables first.
var winnerName stringandvar winnerSalary intstart at the zero values,""and0. They must exist before theif, because a variable declared inside a branch is gone after its closing brace. - Compare with
>=. Writeif productivity1 >= productivity2 {. The>=is what hands a tie to employee 1. - Record employee 1's name in
winnerNameinside the branch. - Record employee 1's salary in
winnerSalary. The name and the salary must always be taken from the same employee. - Add the
else. In Go it sits on the same line as the closing brace:} else {. It runs when employee 2 is strictly more productive. - Record employee 2's name in
winnerName. - Record employee 2's salary in
winnerSalary. - Compute the bonus below the closing brace:
winnerSalary * 15 / 100, with the multiplication before the integer division. - Print the name with
fmt.Println. - Print the bonus line, converting the number with
strconv.Itoabefore joining it to"Bonus: $".
The solution in Go
name1 := "John Doe"
productivity1 := 83
salary1 := 135000
name2 := "Mary Jane"
productivity2 := 133
salary2 := 335000
var winnerName string
var winnerSalary int
if productivity1 >= productivity2 {
winnerName = name1
winnerSalary = salary1
} else {
winnerName = name2
winnerSalary = salary2
}
bonus := winnerSalary * 15 / 100
fmt.Println(winnerName)
fmt.Println("Bonus: $" + strconv.Itoa(bonus))
The code uses the fmt and strconv packages.
The details worth knowing
- Scope. A name declared with
:=orvarinside{ }exists only until the closing brace. If the branches saidwinnerName := name1, they would declare new variables that vanish at the brace, and the laterfmt.Println(winnerName)would fail withundefined: winnerName. Each branch would also reportwinnerName declared and not used. That is why the lesson declares the variables outside and uses plain=inside. varwith a type and no value.var winnerSalary intgives0andvar winnerName stringgives the empty string. Go has no uninitialised variables.- No parentheses, required braces. The condition is written without parentheses, and the braces are mandatory even for one statement. The
elsemust start on the same line as the}, otherwise the code does not compile. - The condition needs a
bool. Go does not treat numbers as true or false, soif productivity1 - productivity2 {is a compile error. You have to write the comparison.
Complexity
The program does a fixed number of operations whatever the values are, so time and extra space are both O(1).
Edge cases and common mistakes
- Ties. With
productivity1 := 100andproductivity2 := 100, the conditionproductivity1 >= productivity2is true, so employee 1 wins. Using>would give the tie to employee 2 and break the rule. - Mixing the pair. Taking the name from one employee and the salary from the other prints the wrong bonus for the winner. Keep both assignments in the same branch.
- Calculating inside the branches. It works, but repeats lines. The lesson's structure keeps the formula in one place.
- Comparing salaries instead of productivity. In the starting data the higher-paid employee also wins, so the mistake would go unnoticed there. Try data where the more productive employee earns less.
Testing
- Productivities 83 and 133 with salaries 135000 and 335000 print
Mary JaneandBonus: $50250. - Productivities 150 and 90 with the same salaries print
John DoeandBonus: $20250. - Productivities 133 and 83 with salaries 100000 and 500000 print
John DoeandBonus: $15000: the more productive employee wins even though the other one earns more. - Productivities 100 and 100 print
John Doe, the first employee.
