mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-11 22:31:36 +00:00
good good study, day day up!
This commit is contained in:
40
vendor/gopl.io/ch5/trace/main.go
generated
vendored
Normal file
40
vendor/gopl.io/ch5/trace/main.go
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 146.
|
||||
|
||||
// The trace program uses defer to add entry/exit diagnostics to a function.
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
//!+main
|
||||
func bigSlowOperation() {
|
||||
defer trace("bigSlowOperation")() // don't forget the extra parentheses
|
||||
// ...lots of work...
|
||||
time.Sleep(10 * time.Second) // simulate slow operation by sleeping
|
||||
}
|
||||
|
||||
func trace(msg string) func() {
|
||||
start := time.Now()
|
||||
log.Printf("enter %s", msg)
|
||||
return func() { log.Printf("exit %s (%s)", msg, time.Since(start)) }
|
||||
}
|
||||
|
||||
//!-main
|
||||
|
||||
func main() {
|
||||
bigSlowOperation()
|
||||
}
|
||||
|
||||
/*
|
||||
!+output
|
||||
$ go build gopl.io/ch5/trace
|
||||
$ ./trace
|
||||
2015/11/18 09:53:26 enter bigSlowOperation
|
||||
2015/11/18 09:53:36 exit bigSlowOperation (10.000589217s)
|
||||
!-output
|
||||
*/
|
Reference in New Issue
Block a user