funcfind

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package funcfind provides static analysis tools for discovering Go functions by their return types.

Overview

FuncFind uses Go's type system and the golang.org/x/tools/go/packages library to analyze Go packages and find functions that return specific types. This is particularly useful for static analysis tools, code generators, and developer utilities that need to understand the structure of Go codebases.

Key Features

  • Type-based function discovery using string matching
  • Support for functions with variable-length return types
  • Lazy evaluation through Go 1.23+ iterators
  • Support for any accessible Go package
  • Memory-efficient processing of large codebases
  • Early termination support for performance

Usage Patterns

The primary function is Returning, which takes a package path and one or more return type strings:

// Find functions returning a single error
functions, err := funcfind.Returning("fmt", "error")
if err != nil {
	return err
}

for fn := range functions {
	fmt.Printf("Function %s returns error\n", fn.Name())
}

// Find functions returning (int, error)
functions, err = funcfind.Returning("fmt", "int", "error")
if err != nil {
	return err
}

for fn := range functions {
	fmt.Printf("Function %s returns (int, error)\n", fn.Name())
}

Type Matching

Function signatures are matched by comparing the string representation of their return types. Functions must match the exact number and order of return types specified. For example:

  • func FnName() error // ✓ matches Returning(pkg, "error")
  • func FnName1() (int, error) // ✓ matches Returning(pkg, "int", "error")
  • func FnName2() (error, int) // ✗ wrong order for Returning(pkg, "int", "error")
  • func FnName5() // ✓ matches Returning(pkg)
  • func FnName4() string // ✗ different type for Returning(pkg, "error")

Performance Considerations

The iterator pattern allows for efficient memory usage and early termination:

// Stop after finding the first match
for fn := range functions {
	if fn.Name() == "target" {
		break // Iterator stops here
	}
}

Limitations

  • Only exported functions are found in external packages
  • Package must be accessible and compilable
  • Type matching is string comparison, order matters
  • No support for interface satisfaction matching

Error Handling

Errors are returned when:

  • Package cannot be loaded (e.g., doesn't exist, compilation errors)
  • Package path is malformed
  • Module resolution fails

Package funcfind provides utilities for discovering Go functions by their return types. It uses static analysis to find functions that return specific types without requiring code compilation or execution.

The primary use case is for static analysis tools that need to discover functions with particular return type signatures across Go packages.

Example usage:

functions, err := funcfind.Returning("fmt", "error")
if err != nil {
	panic(err)
}

for fn := range functions {
	fmt.Printf("Function %s returns error\n", fn.Name())
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Returning

func Returning(pkgPath string, returnTypes ...string) (iter.Seq[*types.Func], error)

Returning finds all functions in the specified Go package that return the provided returnTypes.

The pkgPath parameter should be a valid Go package path (e.g., "fmt", "encoding/json", "github.com/user/repo/pkg"). The returnTypes parameter should be the string representation of the desired return types (e.g., "error", "string", "github.com/user/repo.CustomType").

Returns an iterator over all matching functions. Should package loading fail, returns a non-nil error.

Example:

// Find all functions in fmt package that return error
functions, err := Returning("fmt", "error")
if err != nil {
	return err
}

for fn := range functions {
	fmt.Printf("Found: %s\n", fn.Name())
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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