Skip to content

Instantly share code, notes, and snippets.

@st3v
Created May 25, 2015 10:56
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 st3v/438d7cf4cab2b95d6466 to your computer and use it in GitHub Desktop.
Save st3v/438d7cf4cab2b95d6466 to your computer and use it in GitHub Desktop.
Single-line function timing in Go
// based on https://twitter.com/francesc/status/602606902187458560
package main
import (
"log"
"time"
)
func main() {
runTask("my-task")
}
func whenDone(msg string, args ...interface{}) func() {
start := time.Now()
return func() {
log.Printf(msg, append(args, time.Since(start))...)
}
}
func runTask(name string) {
defer whenDone("Done running '%v'. Total time: %v.", name)()
log.Printf("Start running '%v' ...", name)
time.Sleep(time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment