Documentation
¶
Overview ¶
Package runtime provides enriched runtime introspection utilities extending the standard runtime package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Caller ¶
Caller returns caller information for the function skip frames up the call stack.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/runtime"
)
func main() {
shortName, fullName, file, line, ok := runtime.Caller(0)
fmt.Println(shortName)
fmt.Println(fullName)
fmt.Println(file)
fmt.Println(line)
fmt.Println(ok)
}
Output: runtime_test.ExampleCaller github.com/foomo/go/runtime_test.ExampleCaller runtime/caller_test.go 18 true
func CallerFunc ¶ added in v0.9.0
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/runtime"
)
func main() {
name, _ := runtime.CallerFunc(0)
fmt.Println(name)
}
Output: ExampleCallerFunc
func Recover ¶ added in v0.8.1
func Recover(fn func()) (err error)
Recover calls fn and converts any panic into a *PanicError. Returns nil if fn does not panic.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/foomo/go/runtime"
)
func main() {
err := runtime.Recover(func() {
panic("something went wrong")
})
var pe *runtime.PanicError
if errors.As(err, &pe) {
fmt.Println(pe.Value)
}
}
Output: something went wrong
func StackTrace ¶
StackTrace captures and formats a stack trace up to the specified number of frames, skipping the given number of initial frames.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/runtime"
)
func main() {
stack := runtime.StackTrace(2, 0)
fmt.Println(stack)
}
Output: github.com/foomo/go/runtime_test.ExampleStackTrace runtime/stacktrace_test.go:13 testing.runExample testing/run_example.go:63
Types ¶
type PanicError ¶ added in v0.8.1
type PanicError struct {
// Value is the original value passed to panic().
Value any
// Stack is the full stack trace at the point of the panic.
Stack string
}
PanicError represents a recovered panic with captured runtime context.
func (*PanicError) Error ¶ added in v0.8.1
func (e *PanicError) Error() string
Error returns a string representation including the panic value and stack trace.
func (*PanicError) Unwrap ¶ added in v0.8.1
func (e *PanicError) Unwrap() error
Unwrap returns the panic value if it implements error, nil otherwise. This enables errors.Is and errors.As to reach through the PanicError wrapper.