caller

package module
v0.0.0-...-3011db3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2025 License: MIT Imports: 7 Imported by: 0

README

go-caller

A lightweight and idiomatic Go library that captures and formats caller information such as file name, line number, function name, and package path.

Perfect for use in:

  • Custom error types
  • Logging wrappers
  • Debugging utilities
  • Tracing and instrumentation

✨ Features

  • Minimal and dependency-free
  • Clean, consistent API with platform-independent file paths
  • Get full or short location (file:line)
  • Extract function name, full function path, and package info
  • Implements fmt.Stringer interface for easy logging
  • Implements json.Marshaler and json.Unmarshaler interfaces for easy JSON serialization
  • Implements slog.LogValuer interface for structured logging

🚀 Usage

import "github.com/balinomad/go-caller"

func someFunc() {
    c := caller.Immediate()
    fmt.Println("Caller location:", c.Location())
    fmt.Println("Short:", c.ShortLocation())
    fmt.Println("Function:", c.Function())
    fmt.Println("Package:", c.PackageName())
    data, err := json.Marshal(c)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("JSON:", string(data))
}

📌 Installation

go get github.com/balinomad/go-caller@latest

📘 API Highlights

Method Description
File() Full file path
Line() Line number
Location() Full location (path/to/file.go:123)
ShortLocation() Short location (file.go:123)
Function() Method/function name only
FullFunction() Full path to method including package
Package() Full import path of the package
PackageName() Last element of the package path
String() Returns ShortLocation() for easy logging
MarshalJSON() Marshal caller info to JSON
UnmarshalJSON() Unmarshal JSON to caller info
LogValue() Construct a slog.Value for structured logging

🔧 Advanced

For custom skipping depth or use with program counters:

c := caller.New(skip)
c := caller.NewFromPC(pc)

⚖️ License

MIT License — see LICENSE file for details.

Documentation

Overview

Package caller provides utilities to extract source code location information (file, line, function, and package) for the current or specified call frame. It is designed for use in logging, error reporting, and debugging with a lightweight and idiomatic API. Caller captures runtime metadata using the Go runtime and formats it in a developer-friendly way.

Example usage:

import "github.com/balinomad/go-caller"

func someFunc() {
	c := caller.Immediate()
	fmt.Println("Caller location:", c.Location())
	fmt.Println("Short:", c.ShortLocation())
	fmt.Println("Function:", c.Function())
	fmt.Println("Package:", c.PackageName())
	data, err := json.Marshal(c)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("JSON:", string(data))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Caller

type Caller interface {
	fmt.Stringer
	json.Marshaler
	json.Unmarshaler
	slog.LogValuer

	// Valid returns true if the caller is usable.
	Valid() bool

	// File returns the file name.
	File() string

	// Line returns the line number.
	Line() int

	// Location returns a formatted string with file:line.
	Location() string

	// ShortLocation returns a formatted string with just filename:line.
	ShortLocation() string

	// Function returns just the function or method name
	// without package prefix.
	Function() string

	// FullFunction returns the full function name including package.
	FullFunction() string

	// Package returns the full import path of the function.
	Package() string

	// PackageName returns the name of the package without the directory.
	PackageName() string
}

Caller provides access to source information about the caller.

func Immediate

func Immediate() Caller

Immediate returns a Caller for the immediate caller of the function that calls Immediate(). Returns nil if the caller cannot be determined.

func New

func New(skip int) Caller

New returns a new Caller with source information populated. The skip parameter specifies the number of stack frames to skip in addition to the default offset. Use 0 to get the immediate caller. Returns nil if the skip is invalid or the caller cannot be determined.

func NewFromPC

func NewFromPC(pc uintptr) Caller

NewFromPC returns a new Caller with source information populated based on the provided program counter. Returns nil if the caller cannot be determined.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL