Calling time.Now() directly makes time-dependent tests brittle: execution timing affects which events survive. A function type turns that clock lookup into an injectable dependency. The named type now represents func() time.Time, while production code supplies time.Now and tests supply a deterministic function. section 11.6
Replace the real clock in tests
Define the dependency as type now func() time.Time and store it on Cache. NewCache initializes now with time.Now; a test can inject a function returning a fixed timestamp before calling TrimOlderThan.
Choose the dependency boundary according to what the code needs:
- Prefer a function type when only one operation, such as retrieving the current time, is required. section 11.6
- Avoid replacing the dependency with a mutable global variable, which prevents isolated parallel tests. section 11.6



