Last active
November 19, 2022 18:26
-
-
Save polynomialspace/01cd5a32ef9f57ecf42adcfe4d5362a1 to your computer and use it in GitHub Desktop.
abusing defer evaluation for tracing https://go.dev/play/p/l9b03ugEgF0 https://go.dev/doc/effective_go#defer
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
) | |
func trace() func() { | |
pc, file, line, _ := runtime.Caller(1) | |
fmt.Printf("%s:%d enter %s\n", file, line, runtime.FuncForPC(pc).Name()) | |
return func() { | |
pc, file, line, _ := runtime.Caller(1) | |
fmt.Printf("%s:%d exit %s\n", file, line, runtime.FuncForPC(pc).Name()) | |
} | |
} | |
func foo() { | |
defer trace()() | |
fmt.Println("lol") | |
} | |
func main() { | |
foo() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment