A defer statement defers the execution of a function until the surrounding function returns. However, if I try to print the time taken to execute the following function, it always prints 0.
func sum() {
start := time.Now()
//expecting to print non zero value but always gets 0
defer fmt.Println(time.Now().Sub(start))
sum := 0
for i := 1; i < 101; i++ {
sum += i
}
time.Sleep(1 * time.Second)
fmt.Println(sum)
}