format

原文:https://pkg.go.dev/go/format@go1.23.0

Package format implements standard formatting of Go source.

​ format 包实现 Go 源代码的标准格式化。

Note that formatting of Go source code changes over time, so tools relying on consistent formatting should execute a specific version of the gofmt binary instead of using this package. That way, the formatting will be stable, and the tools won’t need to be recompiled each time gofmt changes.

​ 请注意,Go 源代码的格式化会随着时间而改变,因此依赖于一致格式化的工具应该执行特定版本的 gofmt 二进制文件,而不是使用此包。这样,格式化将是稳定的,并且工具不必在每次 gofmt 更改时重新编译。

For example, pre-submit checks that use this package directly would behave differently depending on what Go version each developer uses, causing the check to be inherently fragile.

​ 例如,直接使用此包的预提交检查的行为会根据每个开发人员使用的 Go 版本而有所不同,从而导致检查本质上很脆弱。

常量

This section is empty.

变量

This section is empty.

函数

func Node

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

Node formats node in canonical gofmt style and writes the result to dst.

​ Node 以规范的 gofmt 样式格式化节点,并将结果写入 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).

​ 节点类型必须是 *ast.File、*printer.CommentedNode、[]ast.Decl、[]ast.Stmt 或与 ast.Expr、ast.Decl、ast.Spec 或 ast.Stmt 兼容的赋值。Node 不修改节点。对于表示部分源文件的节点(例如,如果节点不是 *ast.File 或未包装 *ast.File 的 *printer.CommentedNode),不会对导入进行排序。

The function may return early (before the entire result is written) and return a formatting error, for instance due to an incorrect AST.

​ 该函数可能会提前返回(在整个结果写入之前)并返回格式错误,例如由于 AST 不正确。

Node Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main

import (
	"bytes"
	"fmt"
	"go/format"
	"go/parser"
	"go/token"
	"log"
)

func main() {
	const expr = "(6+2*3)/4"

	// parser.ParseExpr parses the argument and returns the
	// corresponding ast.Node.
	node, err := parser.ParseExpr(expr)
	if err != nil {
		log.Fatal(err)
	}

	// Create a FileSet for node. Since the node does not come
	// from a real source file, fset will be empty.
	fset := token.NewFileSet()

	var buf bytes.Buffer
	err = format.Node(&buf, fset, node)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(buf.String())

}
Output:

(6 + 2*3) / 4

func Source

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

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.

​ Source 以规范的 gofmt 样式格式化 src,并返回结果或(I/O 或语法)错误。src 预计是语法正确的 Go 源文件或 Go 声明或语句的列表。

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.

​ 如果 src 是部分源文件,则 src 的前导和尾随空格将应用于结果(使其具有与 src 相同的前导和尾随空格),并且结果的缩进量与包含代码的 src 的第一行相同。不会对部分源文件的导入进行排序。

类型

This section is empty.