Start learning free

LESSON 16 OF 338 · CALCULATIONS WITH NUMBERS

Area of a Rectangle in Go: Multiplying Two Variables

Lesson Read the full explanation

We created two variables to store the width and the height of a rectangle.

On line 3, use these two variables and an arithmetic operation you just learned to print the area of the rectangle to the console.

STEP 1 OF 1
    Ctrl + Enter to check

    GO · CALCULATIONS WITH NUMBERS

    Area Of Rectangle in Go, explained

    The task

    This exercise puts arithmetic to work. The program already has two variables for a rectangle: width is 23 and height is 17. Your job is to print the rectangle's area.

    The area of a rectangle is its width multiplied by its height. For a 23 by 17 rectangle that is 23 * 17 = 391.

    Using the variables in a calculation

    In Go you never have to write the numbers again. An operator works on variables the same way as on literals, because Go substitutes each variable's current value. So width * height is the product of 23 and 17, and if the variables ever change, the expression follows them. That is the benefit of calculating with names rather than numbers.

    The result of the expression can be given to fmt.Println directly. You don't need an extra variable for the area unless you want to use it more than once.

    Step by step

    1. Look at the starter code. Both variables are declared as int, so they hold whole numbers.
    2. Write the calculation. The operator for multiplication is *, so the expression is width * height.
    3. Print it. Put the expression inside the parentheses of fmt.Println, on the next line after the declarations.
    var width int = 23
    var height int = 17
    
    fmt.Println(width * height)
    

    The program prints 391.

    Things to watch

    • Use *, not x. Go has no multiplication sign other than the asterisk. Writing width x height is a syntax error.
    • Write the names exactly. A misspelling such as widht gives undefined: widht.
    • Don't put the expression in quotes. "width * height" is text, and the program would print those characters unchanged.
    • Keep the types in step. Both variables are int, so their product is an int. If one were a float64 and the other an int, Go would refuse to multiply them until you converted one.

    Storing the area first

    The expression can also be saved in a variable of its own. That is useful when you want to use the result several times, or to give it a meaningful name:

    area := width * height
    fmt.Println(area)                              // 391
    fmt.Println("The area is " + strconv.Itoa(area)) // The area is 391
    

    Since area is an int, joining it to a string with + needs strconv.Itoa. Passing several arguments, as in fmt.Println("The area is", area), prints the same sentence without a conversion.

    One thing to remember about a stored result: it is a snapshot. If you change width to 10 afterwards, area still holds 391. The expression width * height now gives 170, but the variable doesn't update by itself. To get the new area you have to calculate it again and assign it.

    Is int big enough?

    Here, yes. An int in Go is a 64-bit integer, so a product like 391 is tiny compared with its limit. Only enormous values would overflow, and when they do, Go wraps around silently instead of reporting an error. For everyday sizes you can ignore this, but it explains why the type matters: the declared type decides what range of results is safe.

    Variations to try

    Once the area works, change the numbers in the declarations and run the program again: the output follows the variables. You can also print the perimeter of the same rectangle. It is twice the sum of the two sides, and parentheses make the addition happen first:

    fmt.Println(2 * (width + height))
    

    With 23 and 17 it prints 80. For a rectangle with decimal sides, use float64 variables. For example, w, h := 2.5, 4.0 gives w * h equal to 10.

    Key takeaways

    • The area of a rectangle is width times height.
    • Arithmetic works on variables, so you don't repeat the numbers.
    • An expression can be passed straight to fmt.Println.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free