Created
May 26, 2015 11:17
-
-
Save russmack/514e05462382c4d669f7 to your computer and use it in GitHub Desktop.
Time Golang functions in a single line of code with logger.
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
// Author: @francesc | |
package main | |
import ( | |
"log" | |
"time" | |
) | |
type Logger struct { | |
Log func(format string, args ...interface{}) | |
} | |
func whenDone() Logger { | |
start := time.Now() | |
return Logger{func(format string, args ...interface{}) { | |
log.Printf(format, append(args, time.Since(start))...) | |
}} | |
} | |
func process(name string) { | |
defer whenDone().Log("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