title: Measure elapsed time with a defer statement in Go
body: <p>I thought of using the <code>defer</code> statement to measure the elapsed time of a program in Go:</p>
  <pre><code>func main() {
      start := time.Now()
      defer fmt.Println(fmt.Sprintf(&quot;The process took %s&quot;, time.Now().Sub(start)))
      ...
  }
  </code></pre>
  <p>I thought this would produce the correct result but it didn't:</p>
  <pre><code>[09:36:55]pc@work:~/test$ go run ./main.go
  Processed 209806/209806 TUF files
  All records are inserted in the database.
  Verifying records are inserted correctly...
  Verified 209806/209806 TUF files
  All records have been inserted and verified successfully.
  The process took 600ns
  [14:24:06]pc@work:~/test$
  </code></pre>
  <p>Even though the process took 5 hours, which is visible from the timestamps on the left, the deferred <code>fmt.Println()</code> statement showed 600 nanoseconds. What am I doing wrong?</p>