Example of how to use defer
in golang as a crude way of measure execution time. Start by defining the timeit
function. The function just prints the duration from the time given by start
.
Using the defer
keyword we can make timeit
execute directly after the function that we want to measure. Passing time.Now()
as argument gives us a timer that measures execution time for the function body.
Calling timedFunction()
should print