start := time.Now()
// do something
spent := time.Now().Sub(start)
It's worth noting that the Go time library is specifically designed so that computer clocks running backwards won't cause `spent` to be a negative duration. A monotonic clock that only ticks forward is used for time comparison and subtractions.
Hehe, story time: I was using exactly this logic to detect hibernation/sleep, especially for laptops. I was surprised when it never triggered, and printed the time, which indeed showed that a long time had passed. So why didn't it trigger? Because IFF both time stamps have monotonic component (internal, not visible on print) then the monotonic stamp is used. Confused me a lot.
Source: https://pkg.go.dev/time#hdr-Monotonic_Clocks