Documentation
¶
Overview ¶
パッケージformatはGoソースコードの標準的なフォーマットを実装します。
Goソースコードのフォーマットは時間とともに変化するため、 一貫したフォーマットに依存するツールは、このパッケージを使う代わりに特定のバージョンのgofmtバイナリを実行する必要があります。 その方法で、フォーマットが安定し、ツールをgoftmtの変更ごとに再コンパイルする必要がなくなります。
たとえば、このパッケージを直接使用するプレサブミットチェックは、 開発者が使用しているGoのバージョンによって異なる動作をするため、不安定になる可能性があります。
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Node ¶
Nodeはソースコードを標準的なgofmtスタイルに整形し、結果をdstに書き込みます。
nodeの型は*[ast.File]、*[printer.CommentedNode]、[][ast.Decl]、[][ast.Stmt] のいずれかである必要があります。 もしくは [ast.Expr]、[ast.Decl]、[ast.Spec]、[ast.Stmt] と互換性のある代入可能な型である必要があります。 Nodeはnodeを変更しません。部分的なソースファイルを表すノードの場合、 (例えば、nodeが*[ast.File] でない場合や*[printer.CommentedNode] が*[ast.File] を包んでいない場合)インポートはソートされません。
関数は早期に(結果が完全に書き込まれる前に)戻って、 正しくないASTのためにフォーマットエラーを返す場合があります。
Example ¶
package main import ( "github.com/shogo82148/std/bytes" "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/go/format" "github.com/shogo82148/std/go/parser" "github.com/shogo82148/std/go/token" "github.com/shogo82148/std/log" ) func main() { const expr = "(6+2*3)/4" // parser.ParseExprは引数を解析し、対応するast.Nodeを返します。 node, err := parser.ParseExpr(expr) if err != nil { log.Fatal(err) } // ノード用のFileSetを作成します。ノードは実際のソースファイルから // 来ないため、fsetは空になります。 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
Types ¶
This section is empty.