Skip to content

Instantly share code, notes, and snippets.

@sspencer
Created September 10, 2016 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sspencer/4e16c9cba078921623b5902fb7566a34 to your computer and use it in GitHub Desktop.
Save sspencer/4e16c9cba078921623b5902fb7566a34 to your computer and use it in GitHub Desktop.
Time a function
// Golang UK Conference 2016 - Mat Ryer - Idiomatic Go Tricks
func StartTimer(name String) func() {
t := time.Now()
log.Println(name, "started")
return func() {
d := time.Now().Sub(t)
log.Println(name, "took", d)
}
}
func FunkyFunc() {
stop := StartTimer("FunkyFunc)
defer stop()
// preceed with actual function
time.Sleep(1 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment