← All languages
Go function of the day
Random

time.Since

Return elapsed time since a given time.

Description

time.Since returns the time elapsed since t. It is shorthand for time.Now().Sub(t) and uses the monotonic clock reading if available for accurate measurement.

time.Since is commonly used for measuring execution time, benchmarking, and logging elapsed durations.

Because time.Since uses the monotonic clock, it returns accurate results even if the system wall clock is adjusted during the measurement.

Arguments

NameDescriptionOptional
t The starting time to measure from. No

Example

start := time.Now()
time.Sleep(100 * time.Millisecond)
fmt.Println(time.Since(start))  // ~100ms

Reference