How to Use time.Since and time.Until in Go

Use time.Since to calculate elapsed time from a past moment and time.Until to calculate remaining time until a future deadline.

Use time.Since to measure elapsed time since a past moment and time.Until to measure time remaining until a future moment. Both functions return a time.Duration representing the difference between the provided timestamp and the current wall clock time.

start := time.Now()
// ... do work ...
elapsed := time.Since(start)

deadline := time.Now().Add(5 * time.Minute)
// ... wait ...
remaining := time.Until(deadline)