Problem overview
This is the first version of a small project that grows over six lessons. Here there is a single employee, stored in three variables: name, productivity and salary. The program computes an end-of-year bonus of 15% of the salary, rounded down to a whole number, and prints two lines: the name, then Bonus: $ followed by the amount.
With salary := 135000, 15% is 20250, so the output is:
John Doe
Bonus: $20250
The variable productivity is not part of the calculation yet. It waits for the next versions, where employees are compared by it.
Approach: percentage with integer math
A percentage of a whole number is the number times the percent, divided by 100. Both salary and the literal 15 are int values, and in Go dividing one int by another gives an int: the fraction is dropped. Integer division truncates toward zero, which for a positive salary is the same as rounding down. No math.Floor and no conversion to float64 is needed.
Printing the result has one Go-specific wrinkle. Go does not join text and numbers with +. Both operands of + must be strings, so the bonus has to be turned into text before it can follow "Bonus: $". The function for that is strconv.Itoa, which stands for "integer to ASCII".
Step by step
- Compute the bonus. Declare
bonuswith:=, assalary * 15 / 100. Multiply first and divide last. Go evaluates*and/from left to right, so the product is formed before the division. - Print the name.
fmt.Println(name)writes the string and a newline, which gives the first output line. - Print the bonus line. Convert
bonuswithstrconv.Itoa(bonus), join it to the label with+and pass the result tofmt.Println.
The solution in Go
name := "John Doe"
productivity := 83
salary := 135000
_ = productivity
bonus := salary * 15 / 100
fmt.Println(name)
fmt.Println("Bonus: $" + strconv.Itoa(bonus))
The code uses two packages, fmt for printing and strconv for the conversion.
The details worth knowing
_ = productivity. Go refuses to compile a function that declares a local variable and never uses it. With onlyproductivity := 83and no later use, the build stops withproductivity declared and not used. Assigning the value to the blank identifier_counts as a use and throws the value away.- Multiply before dividing. The order matters with integers. For
salary := 135050,salary * 15 / 100gives 20257, whilesalary / 100 * 15divides first, drops 50 and gives 20250. Multiplying first keeps every digit until the single final division. strconv.Itoaand the type error. Writing"Bonus: $" + bonuswith anintvariable fails to compile withinvalid operation: "Bonus: $" + bonus (mismatched types untyped string and int). Go does not convert between types behind your back.- Do not use
string(bonus). Converting an integer withstring(...)treats it as a character code, not as digits:string(rune(65))is"A", whilestrconv.Itoa(65)is"65". OnlyItoaproduces the digits. - A
Printlnalternative.fmt.Println("Bonus: $", bonus)accepts several arguments of any type, but it separates them with a space and printsBonus: $ 20250. The lesson's expected line has no space after the dollar sign, so the explicit conversion is the way.fmt.Printf("Bonus: $%d\n", bonus)also givesBonus: $20250. - The
$is plain text. It has no special meaning inside a Go string.
Edge cases and common mistakes
- Dividing by 100 too early. As shown above,
salary / 100 * 15loses the remainder before the multiplication. - Forgetting the conversion. This is the most common compile error in the lesson: joining the label and the number without
strconv.Itoa. - Removing the blank assignment. If you delete
_ = productivityin this version, the program no longer builds. - Salaries that are not multiples of 20. A salary of 135001 gives 15% = 20250.15, and the program prints 20250. The fraction is cut off, not rounded to the nearest.
- Overflow. An
intis 64 bits in Go 1.22 on a 64-bit machine, so a salary times 15 only overflows near 6e17. Ordinary salaries are nowhere near it.
Testing
salary := 135000printsBonus: $20250.salary := 100printsBonus: $15.salary := 10printsBonus: $1, because 1.5 is cut to 1.salary := 0printsBonus: $0.
Change the values in the editor above and compare the output with these results.
