> For the complete documentation index, see [llms.txt](https://rodneycheung.gitbook.io/handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rodneycheung.gitbook.io/handbook/3.-yu-yan-pian/golang/huan-jing-pei-zhi/dai-ma-jian-cha-ge-shi-hua-gong-ju/gofmt.md).

# Go Fmt

`go fmt`用于形成规范的代码格式，例如在`variable`和`operator`之间加入空格。

## 一、用法

```
usage: gofmt [flags] [path ...]
  -cpuprofile string
        write cpu profile to this file
  -d    display diffs instead of rewriting files
  -e    report all errors (not just the first 10 on different lines)
  -l    list files whose formatting differs from gofmt's
  -r string
        rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
  -s    simplify code
  -w    write result to (source) file instead of stdout
```

一般用法：

```
gofmt -w /path/to/file
```

也可以使用：

```
go fmt /path/to/file
```

该命令相当于`gofmt -w -l /path/to/file`。

## 二、Goland集成

`Goland`内置了`gofmt`的功能，点击`Tools->Go Tools->Go Format Project`即可对整个项目应用`go fmt`。如果想设置文件编辑后自动执行`go fmt`，可以参考，添加配置时使用默认的设置即可。
