Go: get the response time of a website

Go: get the response time of a website

In this article, we'll explore how to get the response time of a website using the Go programming language.

Response time is a critical metric for evaluating website performance. Represents the elapsed time between when a user makes an HTTP request and when the server replies with the data. A quick response is essential to provide a good user experience and improve ranking website on search engines. In this article, we'll explore how to get the response time of a website using the Go programming language.

What is Go?

Go, also known as Golang, is an open source programming language developed by Google. It is valued for its efficiency, ease of learning, and ability to write high-performance programs. Thanks to its powerful standard library, Go is an excellent choice for developing web performance monitoring tools and scripts, including response time measurement.

Write the code

To get the response time of a website, we will use Go to send an HTTP request to the server and measure the time elapsed between sending the request and receiving the response. Here is a simple code example in Go to do this:

package main

     import (
         "fmt"
         "net/http"
         "time"
     )
    
     func main() {
         url := "https://www.example.com" // Replace with your website URL
    
         startTime := time.Now()
    
         response, err := http.Get(url)
         if err != nil {
             fmt.Println("Error in HTTP request:", err)
             return
         }
         defer response.Body.Close()
    
         responseTime := time.Since(startTime).Milliseconds()
         fmt.Printf("Response from %s: %d ms\n", url, responseTime)
     }
     

In this code, we are importing the net/http package to make an HTTP request and the time package to measure time. Replace the example URL with the URL of the website you want to test.

Run the code

Save the code in a file with the ".go" extension (for example, response.go) and run the program using the go run command:

go run response.go

The program will make an HTTP request to your website and return the response time in milliseconds.

Conclusions

Measuring a website's response time is an essential step in evaluating and optimizing your site's performance. Using the Go programming language, you can easily write a script to monitor response time and identify any performance issues. With this tool, you will be able to optimize your website for a better user experience and increased visibility on search engines.