package parse import ( "fmt" "runtime" "strings" ) func callerBefore(name string) string { found := false for i := 1; ; i++ { pc, file, line, ok := runtime.Caller(i) if found { return fmt.Sprintf("%s:%d", file, line) } if !ok { return "unknown caller" } f := runtime.FuncForPC(pc) if strings.HasSuffix(f.Name(), "."+name) { found = true } } } func callerFilepos(depth int) string { // No error handling, because we call this method ourselves with safe depth values. _, file, line, _ := runtime.Caller(depth + 1) return fmt.Sprintf("%s:%d", file, line) } func callerPanic(name, f string, args ...interface{}) { filepos := callerBefore(name) m := fmt.Sprintf(f, args...) m = strings.Replace(m, "{caller}", filepos, -1) m = strings.Replace(m, "{name}", name, -1) panic(m) }