Introduction
A typing speed test is a simple yet effective way to measure how fast you can type text on your keyboard. It is widely used to gauge typing proficiency, whether for professional purposes or casual practice. This project demonstrates how to create a basic typing speed test application using the Go programming language.
Objective
The objective of this program is to create an application that prompts the user to type a given passage as fast as they can. The program will then calculate and display the typing speed in words per minute (WPM) along with the accuracy. It will also display the time taken for completion and highlight any mistakes made by the user.
Code Implementation
package main
import (
“fmt”
“strings”
“time”
)
func main() {
// Define the test text
testText := “The quick brown fox jumps over the lazy dog.”
// Display the test text to the user
fmt.Println(“Typing Speed Test”)
fmt.Println(“——————“)
fmt.Println(“Type the following text:”)
fmt.Println(testText)
fmt.Println()
// Record the start time
startTime := time.Now()
// Get user input
fmt.Print(“Start typing: “)
var userInput string
fmt.Scanln(&userInput)
// Calculate the time taken to complete the typing
endTime := time.Now()
timeTaken := endTime.Sub(startTime)
// Check for any errors in the input
errors := 0
for i := 0; i < len(testText); i++ { if i >= len(userInput) || testText[i] != userInput[i] {
errors++
}
}
// Calculate words per minute
wordsTyped := len(strings.Fields(userInput))
minutes := timeTaken.Seconds() / 60
// Calculate typing speed and accuracy
wpm := float64(wordsTyped) / minutes
accuracy := (float64(len(testText)-errors) / float64(len(testText))) * 100
// Display results
fmt.Printf(“\nTime taken: %.2f seconds\n”, timeTaken.Seconds())
fmt.Printf(“Typing speed: %.2f WPM\n”, wpm)
fmt.Printf(“Accuracy: %.2f%%\n”, accuracy)
}
Explanation of Program Structure
The program is structured in a simple, linear manner. The steps of the program are as follows:
- Define the test text: A fixed sentence is provided for the user to type.
- Display instructions: The program prompts the user to type the text.
- Record the start time: The program starts measuring the time as soon as the user begins typing.
- Get user input: The program accepts the user’s typed input.
- Calculate time taken: The program calculates how long the user took to type the passage.
- Count errors: The program compares the user’s input with the original text and counts errors.
- Calculate typing speed and accuracy: Words per minute (WPM) and accuracy are calculated based on the user’s performance.
- Display results: Finally, the program outputs the time taken, typing speed, and accuracy to the user.
How to Run the Program
To run the program on your local machine, follow these steps:
- Ensure that you have the Go programming language installed. You can download it from here.
- Create a new file called
typing_speed_test.go
on your local system and paste the provided code into it. - Open your terminal or command prompt and navigate to the directory containing the file.
- Run the program by typing
go run typing_speed_test.go
in the terminal. - Follow the instructions provided by the program and start typing the passage as fast and accurately as possible.
- The program will then display your typing speed and accuracy.