package gofix

import "golang.org/x/tools/internal/gofix"

Package gofix defines an Analyzer that inlines calls to functions and uses of constants marked with a "//go:fix inline" directive.

Analyzer gofix

gofix: apply fixes based on go:fix comment directives

The gofix analyzer inlines functions and constants that are marked for inlining.

## Functions

Given a function that is marked for inlining, like this one:

//go:fix inline
func Square(x int) int { return Pow(x, 2) }

this analyzer will recommend that calls to the function elsewhere, in the same or other packages, should be inlined.

Inlining can be used to move off of a deprecated function:

// Deprecated: prefer Pow(x, 2).
//go:fix inline
func Square(x int) int { return Pow(x, 2) }

It can also be used to move off of an obsolete package, as when the import path has changed or a higher major version is available:

package pkg

import pkg2 "pkg/v2"

//go:fix inline
func F() { pkg2.F(nil) }

Replacing a call pkg.F() by pkg2.F(nil) can have no effect on the program, so this mechanism provides a low-risk way to update large numbers of calls. We recommend, where possible, expressing the old API in terms of the new one to enable automatic migration.

The inliner takes care to avoid behavior changes, even subtle ones, such as changes to the order in which argument expressions are evaluated. When it cannot safely eliminate all parameter variables, it may introduce a "binding declaration" of the form

var params = args

to evaluate argument expressions in the correct order and bind them to parameter variables. Since the resulting code transformation may be stylistically suboptimal, such inlinings may be disabled by specifying the -gofix.allow_binding_decl=false flag to the analyzer driver.

(In cases where it is not safe to "reduce" a call—that is, to replace a call f(x) by the body of function f, suitably substituted—the inliner machinery is capable of replacing f by a function literal, func(){...}(). However, the gofix analyzer discards all such "literalizations" unconditionally, again on grounds of style.)

## Constants

Given a constant that is marked for inlining, like this one:

//go:fix inline
const Ptr = Pointer

this analyzer will recommend that uses of Ptr should be replaced with Pointer.

As with functions, inlining can be used to replace deprecated constants and constants in obsolete packages.

A constant definition can be marked for inlining only if it refers to another named constant.

The "//go:fix inline" comment must appear before a single const declaration on its own, as above; before a const declaration that is part of a group, as in this case:

const (
   C = 1
   //go:fix inline
   Ptr = Pointer
)

or before a group, applying to every constant in the group:

//go:fix inline
const (
	Ptr = Pointer
    Val = Value
)

The proposal https://go.dev/issue/32816 introduces the "//go:fix" directives.

You can use this (officially unsupported) command to apply gofix fixes en masse:

$ go run golang.org/x/tools/internal/gofix/cmd/gofix@latest -test ./...

(Do not use "go get -tool" to add gopls as a dependency of your module; gopls commands must be built from their release branch.)

Analyzer gofixdirective

gofixdirective: validate uses of gofix comment directives

The gofixdirective analyzer checks "//go:fix inline" directives for correctness. See the documentation for the gofix analyzer for more about "/go:fix inline".

Index

Variables

var Analyzer = &analysis.Analyzer{
	Name: "gofix",
	Doc:  analysisinternal.MustExtractDoc(doc, "gofix"),
	URL:  "https://pkg.go.dev/golang.org/x/tools/internal/gofix",
	Run:  run,
	FactTypes: []analysis.Fact{
		(*goFixInlineFuncFact)(nil),
		(*goFixInlineConstFact)(nil),
		(*goFixInlineAliasFact)(nil),
	},
	Requires: []*analysis.Analyzer{inspect.Analyzer},
}

Source Files

doc.go gofix.go

Directories

PathSynopsis
internal/gofix/cmd
internal/gofix/cmd/gofixThe gofix command applies the inliner to the specified packages of Go source code.
internal/gofix/findgofixPackage findgofix searches for and validates go:fix directives.
Version
v0.34.0 (latest)
Published
Jun 5, 2025
Platform
js/wasm
Imports
18 packages
Last checked
17 seconds ago

Tools for package owners.