The task
Two variables hold the time spent on a project: hours and minutes. The program must print the whole duration in seconds.
In the example, 1 hour and 2 minutes make 3720 seconds. The lesson's variables hold 13 hours and 47 minutes.
Thinking in units
Unit conversions are easiest in steps. You know two facts: one hour is 60 minutes, and one minute is 60 seconds. So:
- Turn the hours into minutes:
hours * 60. - Add the minutes you already have:
hours * 60 + minutes. Now the whole duration is in a single unit, minutes. - Turn that total into seconds: multiply by 60 again.
With the example, 1 hour and 2 minutes: 1 * 60 + 2 is 62 minutes, and 62 times 60 is 3720 seconds. The conversion is correct because the hours and the minutes were first combined into one unit.
Why parentheses are needed
The second multiplication must apply to the whole sum, not only to minutes. Since * ranks above +, writing the expression without parentheses gives a different calculation:
hours := 1
minutes := 2
fmt.Println((hours * 60 + minutes) * 60) // 3720
fmt.Println(hours * 60 + minutes * 60) // 180
The second line multiplies each part on its own and adds the results, which gives 60 + 120 = 180: the hours were converted to minutes but never to seconds. Parentheses make the addition happen first.
There is another correct way to write the same thing, which does not need parentheses: hours * 3600 + minutes * 60. An hour has 3600 seconds, and a minute has 60. For 1 hour and 2 minutes that is 3600 + 120 = 3720, the same answer. It works because multiplication distributes over addition.
The lesson's exercise
The starting code declares the two values as integers:
- Declare the variables.
var hours int = 13andvar minutes int = 47state the typeintexplicitly. It is the same ashours := 13, where Go infersintfrom the literal. - Build the total in minutes with
hours * 60 + minutes. That is 13 * 60 + 47 = 827. - Wrap it in parentheses and multiply by 60. The result is 827 * 60 = 49620.
- Print it with
fmt.Println.
var hours int = 13
var minutes int = 47
fmt.Println((hours * 60 + minutes) * 60)
The output is 49620. A quick sanity check: 13 hours is 46800 seconds, 47 minutes is 2820 seconds, and together they make 49620.
Why int is a good choice here
All the values are whole numbers, and multiplication and addition of integers are exact, so no precision is lost. Go's int is 64 bits wide on this platform, which is far more than any realistic number of seconds needs, so overflow is not a concern for values like these.
If you ever need the result in larger units again, the same idea runs backwards: divide by 60 for minutes, and use % to get the seconds left over.
Common mistakes and gotchas
- Missing parentheses.
hours * 60 + minutes * 60prints 3600 for the lesson's values instead of 49620, because the hours are only converted to minutes. Check the expression against the statement's example, where 1 and 2 must give 3720. - Mixing units. Adding
hoursandminutesdirectly, as inhours + minutes, adds numbers that measure different things. - Using 100 instead of 60. Time uses base 60, not base 10.
- Unused variables. If you declare a variable in your own program and never read it, the compiler refuses to build. Here both are used in the print expression.
- Forgetting to print. An expression on its own line does nothing; Go also rejects a bare expression statement such as
hours * 60. Pass it tofmt.Println.
Key takeaways
- Convert in steps: hours to minutes, add minutes, minutes to seconds.
- Parentheses make a sum run before a multiplication.
hours * 3600 + minutes * 60is an equivalent form.- Integer arithmetic with whole numbers is exact, so
intsuits this task.