05.Go代码风格的唯一标准
Go 语言设计的目标之一就是解决大型软件系统的大规模开发的问题,解决大型团队的开发问题,Go 核心团队给它起了一个名字叫:规模化(scale)。
gofmt 是伴随着 Go 语言诞生的第一批在“规模化”这个目标上的实践和尝试。gofmt 将统一的代码风格内化到 Go 语言之中,并和 Go 语言一起推广给所有 Go 开发者。在一致的代码风格下,Go 开发人员阅读和维护他人代码时,效率大幅。gofmt 代码风格已经成为 Go 开发者的一种共识。如果你是Go 开发人员,提交代码前使用 gofmt 格式化你的 Go 源码。
<h1>使用 gofmt</h1>Go 1.14 之后,gofmt 工具是放在 Go 安装包中与 Go 编译器工具一并发布的,这足以说明 gofmt 工具的重要程度,gofmt 保持了 Go 语言“简单”的设计哲学,这点通过其帮助手册即可看得出来:
<pre><code class="lang-go hljs">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</code></code></pre>一 使用 gofmt -s 简化你的代码
在 gofmt 中提供了-s 选项,通过 gofmt -s 可以自动将遗留代码中的非简化代码自动转换为简化写法,并且没有副作用,因此一般“-s”选项都会是 gofmt 执行的默认选项。
二 使用 gofmt -r 执行代码“迭代更新”
代码更新迭代和重构是软件工程过程中的日常操作
<pre><code class="lang-go hljs">-r string rewrite rule (e.g., 'a[b:len(a)] -> a[b:]’)</code></code></pre>gofmt -r 的原理就是在对源码进行重新格式化之前,搜索源码是否有可以匹配 pattern 的表达式,如果有,将所有匹配到的结果替换为 replacement 表达式。gofmt 要求 pattern 和 replacement 都是合法的 Go 表达式。比如:
<pre><code class="lang-go hljs">gofmt -r ‘p[6:len(a)] -> p[6:]' -w person.go</code></code></pre>上面 gofmt -r 命令执行的意图就是将源码文件 person.go 中能与 p[6:len(a)] 匹配的代码替换为 p[6:],然后再做重新格式化。
注意:上述命令中的 p 并不是一个具体的字符,而是代表的一个通配符。出现在‘pattern -> replacement’的小写字母都会被视为通配符。因此上面的命令对下面的源码片段都可以成功匹配:
三 使用 gofmt -l 按格式要求输出文件列表
gofmt 提供了-l 选项,可以按格式要求输出满足条件的文件列表。
以上命令并不是只能单独使用,我们也可以将他们组合起来使用。注:如果某路径下有很多不符合 gofmt 格式的文件,这些文件也有可能会被一并输出。
<h1>3. 使用 go-imports</h1>Go 编译器在编译源码时会对源码文件 import 的 package 进行检查:
1 源文件中没有使用但却导入了的 package
2 使用了但没有导入的包
以上两种情况Go 编译器会报错。
那么有没有办法解决呢? 于是,goimports闪亮登场了,goimports 在 gofmt 的功能的基础上,增加了对 package 列表的维护功能,可根据源码的最新变动自动从导入包列表中增删包。安装 goimports 的方法很简单:
如果 go 编译器在$GOPATH/bin 路径里,就会将 goimports 可执行文件放入该路径下,应该保证该路径在$PATH 中即可。goimports 可以理解为 gofmt 上又封装了一层,并且 goimports 的命令行选项和参数与 gofmt 也十分类似:
<pre><code class="lang-go hljs">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</code></code></pre><h1>4. 将 gofmt/goimports 与编辑器工具集成</h1>日常Go 开发人员多使用各种主流编辑器进行代码的编写、测试和重构工作,对代码的格式化一般是通过将 gofmt/goimports 与编辑器集成后在源文件保存时由编辑器自动调用 gofmt/goimports 完成的,几乎不需要手工敲入 gofmt 命令对源码进行格式化的。
goland是目前主流的Go语言开发工具,以此为例子,如下图所示:
<span class="img-wrap"></span>
<span class="img-wrap"></span>
到此这篇关于“05.Go代码风格的唯一标准”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!
您可能感兴趣的文章:
05.Go代码风格的唯一标准
彻底理解GO语言go mod,看了那么多废话却抓不住重点的文章,该看看这个了
PHP的标识有哪几种
Go 语言学习第一章节
Go 语言十年而立,Go2 蓄势待发
Go语言爱好者周刊:第 78 期 — 这道关于 goroutine 的题
php生成唯一标识符的代码
图解 Go 并发编程
Golang的魅力
go run main.go 参数_Go语言入门:Hello world