HackerTrans
TopNewTrendsCommentsPastAskShowJobs

Go Pop quiz: what time was it?(dave.cheney.net)

2 points·by kristianp·gisteren·3 comments
dave.cheney.net
Go Pop quiz: what time was it?

https://dave.cheney.net/2025/12/18/pop-quiz-what-time-was-it

3 comments

yruzin·gisteren
Spoiler for the quiz below.

The defer delays the function call, but it does not delay evaluation of the call’s arguments. This means that both timestamps are therefore captured near the beginning of the request, even though the second line is printed when the handler returns. Also, time.Since can use the monotonic component stored in time.Time, so the duration is not distorted by wall-clock corrections.

start := time.Now() defer func() { log.Printf("request completed in %s", time.Since(start)) }()

The answer is A: the two timestamps are approximately the same.

Thanks for posting this! This is fun :)
kristianp·gisteren
Ok. but why are you posting a spoiler?
yruzin·gisteren
Sorry, my bad :)