05.Go代码风格的唯一标准

本文视频地址

gofmt

Go 语言设计的目标之一就是解决大型软件系统的大规模开发的问题,解决大型团队的开发问题,Go 核心团队给它起了一个名字叫:规模化(scale)。

gofmt 是伴随着 Go 语言诞生的第一批在“规模化”这个目标上的实践和尝试。gofmt 将统一的代码风格内化到 Go 语言之中,并和 Go 语言一起推广给所有 Go 开发者。在一致的代码风格下,Go 开发人员阅读和维护他人代码时,效率大幅。gofmt 代码风格已经成为 Go 开发者的一种共识。如果你是Go 开发人员,提交代码前使用 gofmt 格式化你的 Go 源码。

使用 gofmt

Go 1.14 之后,gofmt 工具是放在 Go 安装包中与 Go 编译器工具一并发布的,这足以说明 gofmt 工具的重要程度,gofmt 保持了 Go 语言“简单”的设计哲学,这点通过其帮助手册即可看得出来:

gofmt -help
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

一 使用 gofmt -s 简化你的代码

在 gofmt 中提供了-s 选项,通过 gofmt -s 可以自动将遗留代码中的非简化代码自动转换为简化写法,并且没有副作用,因此一般“-s”选项都会是 gofmt 执行的默认选项。

二 使用 gofmt -r 执行代码“迭代更新”

代码更新迭代和重构是软件工程过程中的日常操作

-r string
        rewrite rule (e.g., 'a[b:len(a)] -> a[b:]’)

gofmt -r 的原理就是在对源码进行重新格式化之前,搜索源码是否有可以匹配 pattern 的表达式,如果有,将所有匹配到的结果替换为 replacement 表达式。gofmt 要求 pattern 和 replacement 都是合法的 Go 表达式。比如:

gofmt -r ‘p[6:len(a)] -> p[6:]' -w person.go

上面 gofmt -r 命令执行的意图就是将源码文件 person.go 中能与 p[6:len(a)] 匹配的代码替换为 p[6:],然后再做重新格式化。
注意:上述命令中的 p 并不是一个具体的字符,而是代表的一个通配符。出现在‘pattern -> replacement’的小写字母都会被视为通配符。因此上面的命令对下面的源码片段都可以成功匹配:

-   fmt.Println(b[6:len(b)])
+   fmt.Println(b[6:])

-   v, err := s.r.Read(b.buf[6:len(b.buf)])
+   v, err := s.r.Read(b.buf[b.end:])

-   foods = append(foods, persons[6:len(persons)])
+   foods = append(foods, persons[6:])

三 使用 gofmt -l 按格式要求输出文件列表
gofmt 提供了-l 选项,可以按格式要求输出满足条件的文件列表。

gofmt -l ~/go/src/github.com/   

/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/main.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/pkg/util/util.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/vendor/github.com/360EntSecGroup-Skylar/excelize/styles.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/vendor/github.com/Unknwon/com/math.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/vendor/github.com/fvbock/endless/endless.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/vendor/github.com/go-openapi/spec/bindata.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/vendor/github.com/modern-go/concurrent/log.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/vendor/github.com/modern-go/concurrent/unbounded_executor.go
/Users/xxx/go/src/github.com/EDDYCJY/go-gin-example/vendor/github.com/modern-go/reflect2/reflect2.go

以上命令并不是只能单独使用,我们也可以将他们组合起来使用。注:如果某路径下有很多不符合 gofmt 格式的文件,这些文件也有可能会被一并输出。

3. 使用 go-imports

Go 编译器在编译源码时会对源码文件 import 的 package 进行检查:
1 源文件中没有使用但却导入了的 package
2 使用了但没有导入的包
以上两种情况Go 编译器会报错。
那么有没有办法解决呢? 于是,goimports闪亮登场了,goimports 在 gofmt 的功能的基础上,增加了对 package 列表的维护功能,可根据源码的最新变动自动从导入包列表中增删包。安装 goimports 的方法很简单:

    go get golang.org/x/tools/cmd/goimports 

如果 go 编译器在 G O P A T H / b i n 路 径 里 , 就 会 将 g o i m p o r t s 可 执 行 文 件 放 入 该 路 径 下 , 应 该 保 证 该 路 径 在 GOPATH/bin 路径里,就会将 goimports 可执行文件放入该路径下,应该保证该路径在 GOPATH/bingoimportsPATH 中即可。goimports 可以理解为 gofmt 上又封装了一层,并且 goimports 的命令行选项和参数与 gofmt 也十分类似:

usage: goimports [flags] [path ...]
  -cpuprofile string
        CPU profile output
  -d    display diffs instead of rewriting files
  -e    report all errors (not just the first 10 on different lines)
  -format-only
        if true, don't fix imports and only format. In this mode, goimports is effectively gofmt, with the addition that imports are grouped into sections.
  -l    list files whose formatting differs from goimport's
  -local string
        put imports beginning with this string after 3rd-party packages; comma-separated list
  -memprofile string
        memory profile output
  -memrate int
        if > 0, sets runtime.MemProfileRate
  -srcdir dir
        choose imports as if source code is from dir. When operating on a single file, dir may instead be the complete file name.
  -trace string
        trace profile output
  -v    verbose logging
  -w    write result to (source) file instead of stdout

4. 将 gofmt/goimports 与编辑器工具集成

日常Go 开发人员多使用各种主流编辑器进行代码的编写、测试和重构工作,对代码的格式化一般是通过将 gofmt/goimports 与编辑器集成后在源文件保存时由编辑器自动调用 gofmt/goimports 完成的,几乎不需要手工敲入 gofmt 命令对源码进行格式化的。

goland是目前主流的Go语言开发工具,以此为例子,如下图所示:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值