Golang
Golang

 

In this tutorial, we will walk you through the process of creating a basic stopwatch application using the Go programming language. This project is ideal for beginners who want to understand the basic concepts of Go while creating a useful program.

Objective of the Program

The main goal of this project is to create a simple stopwatch that can start, stop, and reset the timer. It will display the elapsed time in seconds and give you an interactive console-based experience. By the end of this tutorial, you should be able to build your own stopwatch application using Go and understand the fundamental concepts of working with time in Go.

Go Program Code: Simple Stopwatch

package main

import (
    "fmt"
    "time"
    "os"
    "strings"
)

func main() {
    var startTime time.Time
    var running bool
    var elapsedTime time.Duration
    var input string

    fmt.Println("Simple Stopwatch in Go")
    fmt.Println("Commands: 'start' to start, 'stop' to stop, 'reset' to reset, 'exit' to quit.")

    for {
        fmt.Print("\nEnter command: ")
        fmt.Scanln(&input)
        input = strings.ToLower(input)

        switch input {
        case "start":
            if running {
                fmt.Println("Stopwatch is already running.")
            } else {
                running = true
                startTime = time.Now()
                fmt.Println("Stopwatch started.")
            }

        case "stop":
            if running {
                elapsedTime = time.Since(startTime)
                running = false
                fmt.Printf("Stopwatch stopped. Time elapsed: %.2f seconds\n", elapsedTime.Seconds())
            } else {
                fmt.Println("Stopwatch is not running.")
            }

        case "reset":
            if running {
                startTime = time.Now()
                elapsedTime = 0
                fmt.Println("Stopwatch reset.")
            } else {
                fmt.Println("Stopwatch is not running.")
            }

        case "exit":
            fmt.Println("Exiting stopwatch program.")
            os.Exit(0)

        default:
            fmt.Println("Invalid command. Try again.")
        }
    }
}

Explanation of the Program

This simple stopwatch program is written in Go and uses several basic Go packages, including fmt for printing, time for handling time-related operations, and os for exiting the program. Here’s an explanation of the key parts:

  • Variables:
    startTime: Stores the start time when the stopwatch is started.
    running: A boolean that keeps track of whether the stopwatch is running.
    elapsedTime: Holds the elapsed time in duration when the stopwatch is stopped.
    input: Captures user input for the commands.
  • Commands: The program continuously asks for user input, which can be one of the following commands:
    • ‘start’: Starts the stopwatch.
    • ‘stop’: Stops the stopwatch and displays the elapsed time.
    • ‘reset’: Resets the stopwatch back to 0 without stopping.
    • ‘exit’: Exits the program.
  • Time Handling: The program uses time.Now() to capture the current time when the stopwatch starts and time.Since() to calculate the elapsed time when the stopwatch is stopped.

How to Run the Program

  1. First, make sure you have Go installed on your system. If you haven’t installed Go, you can download it from the official Go website: https://golang.org/dl/
  2. Create a new file called stopwatch.go and paste the code above into it.
  3. Open your terminal and navigate to the folder where the file is saved.
  4. Run the program by executing the following command:
    go run stopwatch.go
  5. Follow the on-screen instructions to interact with your stopwatch!
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)