format

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package format exposes gofumpt's formatting in an API similar to go/format. In general, the APIs are only guaranteed to work well when the input source is in canonical gofmt format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func File

func File(fset *token.FileSet, file *ast.File, opts Options)

File modifies a file and fset in place to follow gofumpt's format. The changes might include manipulating adding or removing newlines in fset, modifying the position of nodes, or modifying literal values.

func Source

func Source(src []byte, opts Options) ([]byte, error)

Source formats src in gofumpt's format, assuming that src holds a valid Go source file.

Types

type Extra

type Extra struct {

	// GroupParams groups function parameters with repeated types.
	GroupParams bool

	// ClotheReturns clothes naked returns in functions with named results.
	ClotheReturns bool
}

Extra is the set of extra formatting rules which are available.

As the formatter evolves, we might add or remove boolean fields here. Go API users who wish to avoid build errors in such cases can use the string API in Extra.Set.

func (*Extra) IsBoolFlag

func (e *Extra) IsBoolFlag() bool

func (*Extra) Set

func (e *Extra) Set(v string) error

func (*Extra) String

func (e *Extra) String() string

type Options

type Options struct {
	// LangVersion is the Go version a piece of code is written in.
	// The version is used to decide whether to apply formatting
	// rules which require new language features.
	// When empty, a default of go1 is assumed.
	// Otherwise, the version must satisfy [go/version.IsValid].
	//
	// When formatting a Go module, LangVersion should typically be
	//
	//     go list -m -f {{.GoVersion}}
	//
	// with a "go" prefix, or the equivalent from `go mod edit -json`.
	LangVersion string

	// ModulePath corresponds to the Go module path which contains the source
	// code being formatted. When formatting a Go module, ModulePath should be
	//
	//     go list -m -f {{.Path}}
	//
	// or the equivalent from `go mod edit -json`.
	//
	// ModulePath is used for formatting decisions like what import paths are
	// considered to be not part of the standard library. When empty, the source
	// is formatted as if it weren't inside a module.
	ModulePath string

	// ExtraRules enables all extra formatting rules, such as grouping function
	// parameters with repeated types together.
	//
	// Deprecated: use [Options.Extra] instead.
	ExtraRules bool

	// Extra allows enabling extra formatting rules which are disabled by default.
	Extra Extra
}

Options is the set of formatting options which affect gofumpt.

Jump to

Keyboard shortcuts

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