How Inlining Works in the Go Compiler

Inlining replaces function calls with their code bodies to improve performance, controlled by the compiler's heuristics or the -l flag.

Inlining in the Go compiler replaces a function call with the function's body to reduce overhead, occurring during the middle-end optimization phase in cmd/compile/internal/inline. The compiler decides whether to inline based on heuristics like function size and call frequency, which can be disabled globally with the -l flag or inspected with -m.

go tool compile -l main.go

This command compiles main.go with inlining disabled, ensuring function calls remain as distinct instructions rather than being expanded inline.