Menu
Coddy logo textTech

Recap - Simple Timer

Part of the Logic & Flow section of Coddy's GO journey — lesson 65 of 68.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a countdown timer application that uses time.NewTicker to count down from a specified duration to zero, displaying progress messages at regular intervals. This challenge will test your ability to combine tickers, loops, and duration calculations to create a functional timing application.

You will receive two inputs:

  • A string containing the countdown duration in the format "value:unit" (e.g., "10:Second" or "3:Minute")
  • A string containing the update interval in the format "value:unit" (e.g., "1:Second")

Your task is to:

  1. Parse the first input by splitting on colons to extract the countdown duration value and unit
  2. Parse the second input by splitting on colons to extract the update interval value and unit
  3. Convert the string values to integers using the strconv package
  4. Create time.Duration objects for both the countdown duration and update interval using the appropriate constants:
    • For "Second": multiply by time.Second
    • For "Minute": multiply by time.Minute
    • For "Hour": multiply by time.Hour
  5. Display the timer header: "=== COUNTDOWN TIMER ==="
  6. Display the timer setup: "Starting countdown from [countdown_duration_object]"
  7. Display the update frequency: "Updates every [update_interval_object]"
  8. Create a ticker using time.NewTicker with the update interval duration
  9. Initialize a remaining time variable with the countdown duration
  10. Use a loop to count down:
    • Wait for each ticker event using <-ticker.C
    • Subtract the update interval from the remaining time
    • If remaining time is greater than 0, display: "Time remaining: [remaining_time_object]"
    • If remaining time is 0 or less, display: "Time's up!" and exit the loop
  11. Stop the ticker using ticker.Stop()
  12. Display completion statistics:
    • "=== TIMER COMPLETE ==="
    • "Original duration: [countdown_duration_object]"
    • "Update interval: [update_interval_object]"
    • "Total updates displayed: [number_of_updates]" (calculate how many times the remaining time was displayed)
  13. Display the completion message: "Countdown timer finished successfully"

Use the time package for ticker and duration operations, the strings package to split input strings, the strconv package to convert strings to integers, and the fmt package for output. Remember that time.NewTicker returns a ticker that sends values on its channel at regular intervals, and you must call ticker.Stop() when you're done to free up resources. This challenge demonstrates how to create a practical countdown timer using Go's timing mechanisms.

Try it yourself

package main

import (
	"fmt"
	"strconv"
	"strings"
	"time"
)

func main() {
	// Read input
	var countdownInput string
	var intervalInput string
	fmt.Scanln(&countdownInput)
	fmt.Scanln(&intervalInput)
	
	// Parse countdown duration input
	countdownParts := strings.Split(countdownInput, ":")
	countdownValue, _ := strconv.Atoi(countdownParts[0])
	countdownUnit := countdownParts[1]
	
	// Parse update interval input
	intervalParts := strings.Split(intervalInput, ":")
	intervalValue, _ := strconv.Atoi(intervalParts[0])
	intervalUnit := intervalParts[1]
	
	// TODO: Write your code below
	// Convert to time.Duration objects using switch statements
	// Create the countdown timer (simulate without actual delays for testing)
	// Implement the countdown loop
	// Display all required messages and statistics
	// Note: For testing purposes, simulate the ticker behavior without real time delays
	
}

All lessons in Logic & Flow