Created
May 25, 2015 10:56
-
-
Save st3v/438d7cf4cab2b95d6466 to your computer and use it in GitHub Desktop.
Single-line function timing in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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