Subtract Duration From Current Time

Got comments?
Subtract Duration From Current Time

Some people new to Go get confused when searching for an option to subtract a time.Duration (e.g. 24 hrs - to derive yesterday) from a certain time. When looking at the time package one sees only an option to "add" time, but not really to "subtract" time. There is a time.Sub function, but it takes a time.Time and returns the elapsed time.Duration instead. When it comes to subtracting a duration from the given time, there does not seem to be an available option. Or does it?

Recall that in arithmetic, subtraction is the same as addition using a negative prefix. Thus deriving "this time yesterday" from "now" is as easy as:

now := time.Now()
fmt.Println(now.Add(-1 * time.Hour * 24)) // This time yesterday

Playground Link