Junior — Middle
How can you determine that a certain request is delayed?
sobes.tech AI
Answer from AI
To determine if a specific request is delayed in Go, you can measure the request's execution time using the time package.
Example:
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
start := time.Now()
resp, err := http.Get("https://example.com")
if err != nil {
fmt.Println("Request error:", err)
return
}
defer resp.Body.Close()
duration := time.Since(start)
fmt.Printf("Request execution time: %v\n", duration)
// If the time exceeds the threshold, consider it delayed
threshold := 500 * time.Millisecond
if duration > threshold {
fmt.Println("The request is delayed")
} else {
fmt.Println("The request completed quickly")
}
}
Thus, by measuring the time from start to end of the request, you can determine whether it exceeds the acceptable threshold and consider the request "delayed".