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 Frame ¶ added in v0.15.0
type Frame struct {
Pkg string // "github.com/foomo/go/runtime"
Inst string // "Memo"; empty for package-level functions
Func string // "Get", "Meemo.func1", "Map[go.shape.int]"
File string // absolute path as recorded at build time
Line int
}
Frame identifies a single call site.
Pkg is the full import path, Inst the receiver type name for methods (empty for package-level functions), and Func the function or method name including any compiler-generated suffix such as ".func1" or "-fm".
func CallFrame ¶ added in v0.15.0
CallFrame returns the call site skip levels above its own caller: skip=0 is the function that called Caller, skip=1 that function's caller, and so on.
Symbolisation is memoised per program counter, so the steady-state cost is one runtime.Callers into a stack array plus a map load, with no allocation.
func (Frame) Name ¶ added in v0.15.0
Name reassembles the qualified name, suitable for the code.function.name attribute. The receiver's pointer marker is not preserved: a method on *Payment and one on Payment both render as "pkg.Payment.Method".
type Memo ¶ added in v0.15.0
type Memo[T any] struct { // contains filtered or unexported fields }
Memo caches a value of type T per call site.
It exists so that per-site derived data — a span name, a preformatted attribute slice, a logger — is computed once for the life of the process rather than on every call. Each Memo is its own namespace, so several may derive different values from the same call site. The zero value is ready to use and a Memo must not be copied after first use.
func (*Memo[T]) Get ¶ added in v0.15.0
Get returns the memoised value for the call site skip levels above Get's caller: skip=0 is the function that called Get.
derive is invoked at most once per call site under normal conditions, but concurrent first calls from the same site may each run it and race to store; derive must therefore be pure and its result safe to share. Values handed out by Get are shared across all calls from that site and must be treated as read-only.
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.