Use time.Since
ID |
go.time_now_sub |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Best Practice |
Language |
Go |
Tags |
best-practice, code-style |
Description
Reports the chained call time.Now().Sub(x). The standard library provides time.Since(x) as
a shorthand for the elapsed time since x.
Rationale
time.Since(t) is defined as time.Now().Sub(t), so the explicit chained form says the same
thing with more words. Preferring time.Since reads better and matches the idiom most Go code uses
for measuring elapsed durations.
// Bad
d := time.Now().Sub(start)
// Good
d := time.Since(start)