Skip to content

Instantly share code, notes, and snippets.

@tywkeene
Last active July 21, 2023 19:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tywkeene/cceab2e03505b403ce89b0adfb5ca8a2 to your computer and use it in GitHub Desktop.
Save tywkeene/cceab2e03505b403ce89b0adfb5ca8a2 to your computer and use it in GitHub Desktop.
Where?
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.
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