package main import ( "fmt" "time" ) func main() { var start time.Time var elapsed time.Duration var running bool go func() { for { time.Sleep(1 * time.Second) if running { elapsed = time.Since(start) fmt.Printf("\rElapsed Time: %02d:%02d:%02d", int(elapsed.Hours()), int(elapsed.Minutes())%60, int(elapsed.Seconds())%60) } } }() for { fmt.Println("> s: Start, p: Pause, r: Reset, q: Quit") var input string fmt.Scanln(&input) switch input { case "s": if !running { start = time.Now().Add(-elapsed) running = true fmt.Println("Stopwatch started.") } case "p": if running { running = false fmt.Printf("\nPaused at: %02d:%02d:%02d\n", int(elapsed.Hours()), int(elapsed.Minutes())%60, int(elapsed.Seconds())%60) } case "r": start = time.Time{} elapsed = 0 running = false fmt.Println("\nStopwatch reset.") case "q": fmt.Println("\nExiting") return default: fmt.Println("\nInvalid input, please try again.") } } }