patterns/go/timer.go

24 lines
353 B
Go
Raw Normal View History

2020-09-29 23:27:16 +00:00
package main
// from https://www.youtube.com/watch?v=yeetIgNeIkc&t=1064s
import (
"log"
"time"
)
func main() {
stop := StartTimer("main")
time.Sleep(1 * time.Second)
defer stop()
}
func StartTimer(name string) func() {
t := time.Now()
log.Println(name, "started")
return func() {
d := time.Now().Sub(t)
log.Println(name, "took", d)
}
}