Go ソースを読み易くする gofmt ツール

Go にはソースファイルを整形する gofmt というツールが付いています。例えば、以下のようなソースがあったとします。

package main
import "fmt" func main() {
fmt.Printf("hello, world\n")//print hello
}


gofmtにこのファイルを指定すると、

$ gofmt hello.go 
package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")    //print hello
}

改行やタブを整えてくれます。デフォルトでは標準出力に出力しますが、以下のように -w オプションでファイルを直接更新します。

$ gofmt -w hello.go 


その他のオプションは以下のようになっています。

usage: gofmt [flags] [path ...]
  -tabwidth=8: tab width
  -trace=false: print parse trace
  -l=false: list files whose formatting differs from gofmt's
  -w=false: write result to (source) file instead of stdout
  -spaces=false: align with spaces instead of tabs
  -comments=true: print comments