Go Timer: Building a Countdown Clock
This challenge requires you to implement a functional countdown timer in Go. You'll learn how to manage time intervals, handle user input, and display dynamic output, skills crucial for building interactive applications.
Problem Description
Your task is to create a Go program that functions as a countdown timer. The program should accept a duration in seconds from the user, then count down from that duration, displaying the remaining time in a human-readable format (minutes and seconds) at one-second intervals.
Key Requirements:
- User Input: The program must prompt the user to enter a duration in seconds.
- Validation: The input must be a non-negative integer. If invalid input is provided, an appropriate error message should be displayed, and the program should exit gracefully.
- Countdown Logic: The timer should decrement the remaining time by one second for each tick.
- Display Format: The remaining time should be displayed in the format "MM:SS" (e.g., "01:30" for 1 minute and 30 seconds).
- Interval: The display should update every second.
- Completion: When the timer reaches zero, a "Time's up!" message should be printed, and the program should terminate.
Expected Behavior:
The program will:
- Print a prompt asking for the duration in seconds.
- Read the user's input.
- Validate the input.
- If valid, start the countdown.
- Print the initial remaining time.
- Every second, update and print the remaining time.
- Upon reaching zero, print the "Time's up!" message and exit.
Edge Cases:
- Zero Duration: If the user enters 0 seconds, the "Time's up!" message should be displayed immediately.
- Large Durations: The program should handle durations that result in minutes greater than 59 (e.g., 3600 seconds should display as "60:00").
Examples
Example 1:
Input:
Enter duration in seconds: 5
Output:
00:05
00:04
00:03
00:02
00:01
Time's up!
Explanation: The user enters 5 seconds. The timer counts down from 00:05 to 00:01, then prints "Time's up!".
Example 2:
Input:
Enter duration in seconds: 65
Output:
01:05
01:04
...
01:00
00:59
...
00:00
Time's up!
Explanation: The user enters 65 seconds. The timer correctly displays the time in minutes and seconds, starting from 01:05 and counting down to 00:00.
Example 3:
Input:
Enter duration in seconds: -10
Output:
Invalid input. Please enter a non-negative integer.
Explanation: The user enters a negative number, which is invalid. The program prints an error message and exits.
Example 4:
Input:
Enter duration in seconds: abc
Output:
Invalid input. Please enter a non-negative integer.
Explanation: The user enters non-numeric input. The program prints an error message and exits.
Constraints
- The duration input will be an integer.
- The duration will not exceed the maximum value of an
intin Go. - The program should be responsive and not exhibit significant delays beyond the one-second interval.
Notes
- Consider using the
timepackage in Go for managing time durations and delays. - The
fmtpackage will be useful for input/output operations. - You might want to use a loop structure to manage the countdown.
- Think about how to clear the console output for a smoother display, or how to manage printing new lines to show the countdown updating. (This is an optional enhancement).