format
3 分钟阅读
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
|
|
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
|
|
func Source
|
|
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.