Documentation
¶
Overview ¶
Package mininline implements the rewrite engine for safe Go function inlining.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanCache ¶
func CleanCache() error
CleanCache removes cached mininline parse results.
Example ¶
ExampleCleanCache clears cached parse results. Clearing an empty cache is safe and needs no setup.
if err := CleanCache(); err != nil {
panic(err)
}
fmt.Println("cache cleaned")
Output: cache cleaned
func PackageDirs ¶
PackageDirs resolves files, directories, and Go package patterns.
Example ¶
ExamplePackageDirs resolves a Go file to its containing package directory. A source file and its package share the same directory in this example.
directory, err := os.MkdirTemp("", "mininline-example-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(directory)
path := filepath.Join(directory, "source.go")
if err := os.WriteFile(path, []byte("package p\n"), 0o644); err != nil {
panic(err)
}
dirs, err := PackageDirs(context.Background(), []string{path}, nil, directory)
if err != nil {
panic(err)
}
fmt.Println("package directories:", len(dirs))
Output: package directories: 1
func Rewrite ¶
Rewrite marks eligible functions in Go files and writes the proposed changes.
Example ¶
ExampleRewrite adds inline markers to selected files in place. Here, `once()` is only called by `caller()`, so it is marked for inlining.
directory, err := os.MkdirTemp("", "mininline-example-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(directory)
path := filepath.Join(directory, "source.go")
if err := os.WriteFile(path, []byte("package p\n\nfunc once() {}\nfunc caller() { once() }\n"), 0o644); err != nil {
panic(err)
}
err = Rewrite(context.Background(), []string{path}, directory)
source, readErr := os.ReadFile(path)
if err != nil {
panic(err)
}
if readErr != nil {
panic(readErr)
}
fmt.Print(string(source))
Output: package p //go:fix inline func once() {} func caller() { once() }
Types ¶
type Change ¶
type Change struct {
Path string
Before []byte
After []byte
Marked []MarkedFunction
}
Change contains a source file before and after mininline's rewrite.
type MarkedFunction ¶
MarkedFunction identifies a function selected for inlining.
type Options ¶
type Options struct {
Env []string
Destructive bool
Failfast bool
Recursive bool
Verbose bool
BailoutAfter time.Duration
Stderr io.Writer
TrivialExpr int64
TrivialExprExemptMinCalls int64
}
Options controls mininline's rewrite behavior.
type Result ¶
type Result struct {
Changes []Change
}
Result contains the source files considered by one mininline run.
func Run ¶
func Run(ctx context.Context, dirs []string, cwd string, mode rewrite.Mode, options Options) (Result, error)
Run marks eligible functions and optionally applies go fix.
Example ¶
ExampleRun reports the function selected for an inlining marker. The single-use `once()` helper is the only function selected.
directory, err := os.MkdirTemp("", "mininline-example-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(directory)
path := filepath.Join(directory, "source.go")
source := "package p\n\nfunc once() {}\nfunc caller() { once() }\n"
if err := os.WriteFile(path, []byte(source), 0o644); err != nil {
panic(err)
}
result, err := Run(context.Background(), []string{directory}, directory, rewrite.ModePrint, Options{})
if err != nil {
panic(err)
}
fmt.Println("selected function:", result.Changes[0].Marked[0].Name)
Output: selected function: once