Skip to content

Instantly share code, notes, and snippets.

@russmack
Created May 25, 2015 16:51
Show Gist options
  • Save russmack/4260a1f8089761e342ac to your computer and use it in GitHub Desktop.
Save russmack/4260a1f8089761e342ac to your computer and use it in GitHub Desktop.
Time Golang functions in a single line of code.
// Author: @francesc
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