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 andtime.Since()
to calculate the elapsed time when the stopwatch is stopped.
How to Run the Program
- 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/
- Create a new file called stopwatch.go and paste the code above into it.
- Open your terminal and navigate to the folder where the file is saved.
- Run the program by executing the following command:
go run stopwatch.go
- Follow the on-screen instructions to interact with your stopwatch!