format

Imports

Imports #

"bytes"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"io"
"bytes"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"strings"

Constants & Variables

config var #

var config = printer.Config{...}

parserMode const #

const parserMode = *ast.BinaryExpr

printerMode const #

Keep these in sync with cmd/gofmt/gofmt.go.

const printerMode = *ast.BinaryExpr

printerNormalizeNumbers const #

printerNormalizeNumbers means to canonicalize number literal prefixes and exponents while printing. See https://golang.org/doc/go1.13#gofmt. This value is defined in go/printer specifically for go/format and cmd/gofmt.

const printerNormalizeNumbers = *ast.BinaryExpr

tabWidth const #

Keep these in sync with cmd/gofmt/gofmt.go.

const tabWidth = 8

Functions

Node function #

Node formats node in canonical gofmt style and writes the result to dst. The node type must be *[ast.File], *[printer.CommentedNode], [][ast.Decl], [][ast.Stmt], or assignment-compatible to [ast.Expr], [ast.Decl], [ast.Spec], or [ast.Stmt]. Node does not modify node. Imports are not sorted for nodes representing partial source files (for instance, if the node is not an *[ast.File] or a *[printer.CommentedNode] not wrapping an *[ast.File]). The function may return early (before the entire result is written) and return a formatting error, for instance due to an incorrect AST.

func Node(dst io.Writer, fset *token.FileSet, node any) error

Source function #

Source formats src in canonical gofmt style and returns the result or an (I/O or syntax) error. src is expected to be a syntactically correct Go source file, or a list of Go declarations or statements. If src is a partial source file, the leading and trailing space of src is applied to the result (such that it has the same leading and trailing space as src), and the result is indented by the same amount as the first line of src containing code. Imports are not sorted for partial source files.

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

format function #

format formats the given package file originally obtained from src and adjusts the result based on the original source via sourceAdj and indentAdj.

func format(fset *token.FileSet, file *ast.File, sourceAdj func(src []byte, indent int) []byte, indentAdj int, src []byte, cfg printer.Config) ([]byte, error)

hasUnsortedImports function #

func hasUnsortedImports(file *ast.File) bool

isSpace function #

isSpace reports whether the byte is a space character. isSpace defines a space as being among the following bytes: ' ', '\t', '\n' and '\r'.

func isSpace(b byte) bool

parse function #

parse parses src, which was read from the named file, as a Go source file, declaration, or statement list.

func parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) (file *ast.File, sourceAdj func(src []byte, indent int) []byte, indentAdj int, err error)

Generated with Arrow