Start learning free

LESSON 55 OF 285 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in Go: One Employee and a Bonus

Practice Read the full explanation

Compute an end-of-year performance bonus for one employee, then print the employee's name and the bonus.

You start with:

name := "John Doe"
productivity := 83
salary := 135000
_ = productivity

Build:

John Doe
Bonus: $20250

Rules:

  • The bonus is 15% of the salary, rounded down to a whole number.
  • Print the employee's name on the first line.
  • Print the bonus on the second line as "Bonus: $<amount>".
  • productivity is not used in this version. It is there for the next versions, where you will compare employees by productivity.

Dividing two whole numbers (int) drops the fraction, so salary * 15 / 100 is already rounded down: bonus := salary * 15 / 100

Join text and a number with strconv.Itoa: fmt.Println("Bonus: $" + strconv.Itoa(bonus))

name := "John Doe"
productivity := 83
salary := 135000
_ = productivity

bonus := salary * 15 / 100
fmt.Println(name)
fmt.Println("Bonus: $" + strconv.Itoa(bonus))

STEP 1 OF 3
    Ctrl + Enter to check

    GO · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in Go, explained

    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

    1. Compute the bonus. Declare bonus with :=, as salary * 15 / 100. Multiply first and divide last. Go evaluates * and / from left to right, so the product is formed before the division.
    2. Print the name. fmt.Println(name) writes the string and a newline, which gives the first output line.
    3. Print the bonus line. Convert bonus with strconv.Itoa(bonus), join it to the label with + and pass the result to fmt.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 only productivity := 83 and no later use, the build stops with productivity 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 / 100 gives 20257, while salary / 100 * 15 divides first, drops 50 and gives 20250. Multiplying first keeps every digit until the single final division.
    • strconv.Itoa and the type error. Writing "Bonus: $" + bonus with an int variable fails to compile with invalid 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 with string(...) treats it as a character code, not as digits: string(rune(65)) is "A", while strconv.Itoa(65) is "65". Only Itoa produces the digits.
    • A Println alternative. fmt.Println("Bonus: $", bonus) accepts several arguments of any type, but it separates them with a space and prints Bonus: $ 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 gives Bonus: $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 * 15 loses 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 _ = productivity in 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 int is 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 := 135000 prints Bonus: $20250.
    • salary := 100 prints Bonus: $15.
    • salary := 10 prints Bonus: $1, because 1.5 is cut to 1.
    • salary := 0 prints Bonus: $0.

    Change the values in the editor above and compare the output with these results.

    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free