Last active
July 21, 2023 19:26
-
-
Save tywkeene/cceab2e03505b403ce89b0adfb5ca8a2 to your computer and use it in GitHub Desktop.
Where?
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
The where() function returns the output: | |
~/tmp/where ❯ go run main.go | |
main.a in "/Users/[redacted]/tmp/where/main.go" returns from line 19 | |
Maybe useful for debugging some large function when you don't have the patience for it. |
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 where() { | |
pc, file, line, _ := runtime.Caller(1) | |
fn := runtime.FuncForPC(pc) | |
fmt.Printf("%s in %q returns from line %d\n", fn.Name(), file, line) | |
} | |
func a(i int) string { | |
defer where() | |
switch i { | |
case 1: | |
return "one" | |
case 2: | |
return "two" | |
case 3: | |
return "three" | |
} | |
return "?" | |
} | |
func main() { | |
a(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment