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. A second analyzer only checks uses of the 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.
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/gopls/internal/analysis/gofix/cmd/gofix@latest -test ./...
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: func(pass *analysis.Pass) (any, error) { return run(pass, true) }, FactTypes: []analysis.Fact{ (*goFixInlineFuncFact)(nil), (*goFixInlineConstFact)(nil), (*goFixInlineAliasFact)(nil), }, Requires: []*analysis.Analyzer{inspect.Analyzer}, }
var DirectiveAnalyzer = &analysis.Analyzer{ Name: "gofixdirective", Doc: analysisinternal.MustExtractDoc(doc, "gofixdirective"), URL: "https://pkg.go.dev/golang.org/x/tools/internal/gofix", Run: func(pass *analysis.Pass) (any, error) { return run(pass, false) }, FactTypes: []analysis.Fact{ (*goFixInlineFuncFact)(nil), (*goFixInlineConstFact)(nil), (*goFixInlineAliasFact)(nil), }, Requires: []*analysis.Analyzer{inspect.Analyzer}, }
Source Files ¶
doc.go gofix.go
Directories ¶
Path | Synopsis |
---|---|
internal/gofix/cmd | |
internal/gofix/cmd/gofix | The inline command applies the inliner to the specified packages of Go source code. |
- Version
- v0.32.0 (latest)
- Published
- Apr 8, 2025
- Platform
- js/wasm
- Imports
- 19 packages
- Last checked
- 2 minutes ago –
Tools for package owners.