mininline

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: BSD-3-Clause Imports: 32 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func PackageDirs(ctx context.Context, paths, env []string, cwd string) ([]string, error)

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

func Rewrite(ctx context.Context, paths []string, cwd string) error

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 ImportType

type ImportType uint8

ImportType describes the form of an import declaration.

type MarkedFunction

type MarkedFunction struct {
	Name     string
	Position token.Position
}

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

Jump to

Keyboard shortcuts

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