3.2. コードのフォーマット
code_path パスのすべてのコードをフォーマットするには、以下のように gofmt ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable go-toolset-1.11 'gofmt -w code_path'
Red Hat Enterprise Linux 8 の場合:
$ gofmt -w code_path
このコマンドは、code_path パスを直接変更します。code_path が 1 つのファイルである場合、変更はファイルにのみ適用されます。code_path がディレクトリーである場合は、ディレクトリー内のすべての .go
ファイルが処理されます。
code_path を省略すると、gofmt は代わりに標準入力を読み込みます。
元のファイルに書き込みせずにフォーマットされたコードを標準出力に出力するには、-w
オプションを省略します。
fmt
コマンドで go コンパイラーで gofmt を呼び出すことができます。上記のコマンドと同じ結果を実現するには、以下のコマンドを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable go-toolset-1.11 'gofmt -l -w -s code_path'
Red Hat Enterprise Linux 8 の場合:
$ gofmt -l -w -s code_path