Created
May 25, 2015 16:23
-
-
Save vgarvardt/a0f80ee456019fb7980f to your computer and use it in GitHub Desktop.
Function elapsed time in golang from http://play.golang.org/p/U58I5eGXRY
This file contains hidden or 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
package main | |
import ( | |
"log" | |
"time" | |
) | |
func whenDone() func(format string, args ...interface{}) { | |
start := time.Now() | |
return func(format string, args ...interface{}) { | |
log.Printf(format, append(args, time.Since(start))...) | |
} | |
} | |
func process(name string) { | |
defer whenDone()("total time processing %v: %v", name) | |
log.Printf("started processing %v", name) | |
time.Sleep(time.Second) | |
} | |
func main() { | |
process("foo") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment