听说这能让代码更优雅?有什么秘诀吗?🔍
- 内容介绍
- 文章标签
- 相关推荐
听说这能让代码更优雅?有什么秘诀吗?🔍
相信很多程序员朋友都听说过静态代码检查工具这个概念, 它可能是我们IDE里的某一个插件,可能是计算机中的一个程序,还可能是Git提交后的一个流程,如果是对代码要求比较高的个人或组织,静态代码检查工具则是一个绕不开的东西。

为什么需要静态代码检查工具?🤔
一个事物的出现必然是有所需求的, 那么我们不妨先思考一下为什么需要静态代码检查工具?
这是可以说的吗? 大家都知道我们写的代码是需要通过编译器编译成中间代码或可施行文件的, 比如Java程序代码会由Java编译器编译成class文件,由JVM施行,Go代码会由Go编译器编译成二进制文件直接施行,中间都会有一个编译的过程,在编译原理中会有两个比较重要的流程——词法分析和语法分析。先说下词法分析和语法分析主要是用来做什么的:
词法分析 🔍
呵... 词法分析是编译过程的第一步, 其主要作用和特点如下:
看这段代码:
package main
import "fmt"
func main {
str := "Hello World"
}施行后报错,主要原因是在程序中我们规定语法是先声明后使用,这段代码却是先使用后声明
.\:6:14: undefined: str
.\:7:2: str declared and not used语法分析 📝
语法分析是在词法分析的基础上进行的其主要作用和特点如下:,太坑了。
太坑了。 再举个例子,我们使用Go中一个fmt包中不存在的函数:
package main
import "fmt"
func main {
}施行后报错,原因很简单,主要原因是函数的单词拼错了:,有啥用呢?
.\:6:6: 现在我们知道了词法分析和语法分析有一个共同的作用:错误监测,放心去做...。
使用内置的go vet工具 🔩
使用内置的**go vet**工具go vet是Go语言内置的一个静态分析工具, 它可以帮助开发者检查Go代码中的潜在问题, 差点意思。 如未使用的变量、错误的使用标志位等。
工具名称
功能简介
go vet
检查Go代码中的潜在问题, 如未使用的变量、错误的使用标志位等。
golangci-lint
统一了多个Go静态代码检查工具,并支持在CI/CD中使用。
errcheck
检查错误返回值的处理。
golangci-lint 🌟-awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏-一大波人在这里讨论xxx话题-awesome Go linter 大一统!👏-一大波人在这里讨论xxx话题-awesome Go linter 大一统!👏-, 点击围观 👉 -awesome Go linter 大一统!👏-, 点击围观 👉 -awesome Go linter 大一统!👏-golangci-lint run :.....$ golangci-lint run ./...
main.go:7:2: printf: format %d has arg str of wrong type string,原来如此。
^
看好你哦! main.go:13:7: Error return value is not checked
hello
^
main.go:11:2: printf: format %d has arg str of wrong type string
^
// ... more output ...
$ .$ golangci-lint run ./...
main.g 得了吧... o:7:2: printf: format %d has arg str of wrong type string
^
^
// ... more output ...
$ .// 可以看到有些是默认支持,有些是默认关闭支持的,我们不妨来试一下它和上面的两个工具有什么不同,我们将上面的两个代码示例合并到一起施行,
golangci-lint help linters查看它都支持哪些静态检查工具:// ... lots of output ... staticcheck✔ Enabled by defaultgosimple✔ Enabled by defaultunused✔ Enabled by default...// ... lots of output ...
$ golangci-lint help linters
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled by default
unused ✔ Enabled by default
...
// ... lots of output ...
$ // 可以看到有些是默认支持,有些是默认关闭支持的,我们不妨来试一下它和上面的两个工具有什么不同,我们将上面的两个代码示例合并到一起施行,
golangci-lint help linters查看它都支持哪些静态检查工具:// ... lots of output ... staticcheck✔ Enabled by defaultgosimple✔ Enabled bydefaultunused✔ Enabled bydefault...// ... lots of output ...
$ golangci-lint help linters
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled by default
unused ✔ Enabled by default
...
// ... lots of output ...
$ // 可以看到有些是默认支持,有些是默认关闭支持的,我们不妨来试一下它和上面的两个工具有什么不同,我们将上面的两个代码示例合并到一起施行,
golangci-lint help linters查看它都支持哪些静态检查工具:
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled bydefault
unused ✔ Enabled bydefault
...
// ... lots of output ...
$ golangci-lint help linters
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled by default
unused ✔ Enabled by default
...
// ... lots of output ...
$
golangci-lint..
总而言之 🎉🎊🙌大吉大利、 我懵了。 今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋如下 📝💡:提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;降低维护成本 💸;确保所有成员都遵循相同的编码规范 👥.能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;降低维护成本 💸;确保所有成员都遵循相同的编码规范 👥..totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;可以发现''一些很秀的操作++;;);)~~ 😉~~ 😉~~ 😉~~ 😉~~ 😉~~ 😉~~ 😉有时候真的是 ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~o).byebye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻下次再见喽~~~ 🐷下次再见喽~~~ 🐷下次再见喽~~~ 🐷下次再见喽~~~ 🐷下次再见喽~~~ 🐷拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰Bye for now! 📠Bye for now! 📠Bye for now! 📠Bye for now! 📠Bye for now! 📠See you soon! 👀See you soon! 👀See you soon! 👀See you soon! 👀See you soon! 👀Later, folks! 👫Later, folks! 👫Later, folks! 👫Later, folks! 👫Later, folks!.byebye~~~ 👋🏻.byebye~~~ 👋🏻.byebye~~~ 👋🏻.byebye~~~
听说这能让代码更优雅?有什么秘诀吗?🔍
相信很多程序员朋友都听说过静态代码检查工具这个概念, 它可能是我们IDE里的某一个插件,可能是计算机中的一个程序,还可能是Git提交后的一个流程,如果是对代码要求比较高的个人或组织,静态代码检查工具则是一个绕不开的东西。

为什么需要静态代码检查工具?🤔
一个事物的出现必然是有所需求的, 那么我们不妨先思考一下为什么需要静态代码检查工具?
这是可以说的吗? 大家都知道我们写的代码是需要通过编译器编译成中间代码或可施行文件的, 比如Java程序代码会由Java编译器编译成class文件,由JVM施行,Go代码会由Go编译器编译成二进制文件直接施行,中间都会有一个编译的过程,在编译原理中会有两个比较重要的流程——词法分析和语法分析。先说下词法分析和语法分析主要是用来做什么的:
词法分析 🔍
呵... 词法分析是编译过程的第一步, 其主要作用和特点如下:
看这段代码:
package main
import "fmt"
func main {
str := "Hello World"
}施行后报错,主要原因是在程序中我们规定语法是先声明后使用,这段代码却是先使用后声明
.\:6:14: undefined: str
.\:7:2: str declared and not used语法分析 📝
语法分析是在词法分析的基础上进行的其主要作用和特点如下:,太坑了。
太坑了。 再举个例子,我们使用Go中一个fmt包中不存在的函数:
package main
import "fmt"
func main {
}施行后报错,原因很简单,主要原因是函数的单词拼错了:,有啥用呢?
.\:6:6: 现在我们知道了词法分析和语法分析有一个共同的作用:错误监测,放心去做...。
使用内置的go vet工具 🔩
使用内置的**go vet**工具go vet是Go语言内置的一个静态分析工具, 它可以帮助开发者检查Go代码中的潜在问题, 差点意思。 如未使用的变量、错误的使用标志位等。
工具名称
功能简介
go vet
检查Go代码中的潜在问题, 如未使用的变量、错误的使用标志位等。
golangci-lint
统一了多个Go静态代码检查工具,并支持在CI/CD中使用。
errcheck
检查错误返回值的处理。
golangci-lint 🌟-awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏--awesome Go linter 大一统!👏-一大波人在这里讨论xxx话题-awesome Go linter 大一统!👏-一大波人在这里讨论xxx话题-awesome Go linter 大一统!👏-, 点击围观 👉 -awesome Go linter 大一统!👏-, 点击围观 👉 -awesome Go linter 大一统!👏-golangci-lint run :.....$ golangci-lint run ./...
main.go:7:2: printf: format %d has arg str of wrong type string,原来如此。
^
看好你哦! main.go:13:7: Error return value is not checked
hello
^
main.go:11:2: printf: format %d has arg str of wrong type string
^
// ... more output ...
$ .$ golangci-lint run ./...
main.g 得了吧... o:7:2: printf: format %d has arg str of wrong type string
^
^
// ... more output ...
$ .// 可以看到有些是默认支持,有些是默认关闭支持的,我们不妨来试一下它和上面的两个工具有什么不同,我们将上面的两个代码示例合并到一起施行,
golangci-lint help linters查看它都支持哪些静态检查工具:// ... lots of output ... staticcheck✔ Enabled by defaultgosimple✔ Enabled by defaultunused✔ Enabled by default...// ... lots of output ...
$ golangci-lint help linters
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled by default
unused ✔ Enabled by default
...
// ... lots of output ...
$ // 可以看到有些是默认支持,有些是默认关闭支持的,我们不妨来试一下它和上面的两个工具有什么不同,我们将上面的两个代码示例合并到一起施行,
golangci-lint help linters查看它都支持哪些静态检查工具:// ... lots of output ... staticcheck✔ Enabled by defaultgosimple✔ Enabled bydefaultunused✔ Enabled bydefault...// ... lots of output ...
$ golangci-lint help linters
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled by default
unused ✔ Enabled by default
...
// ... lots of output ...
$ // 可以看到有些是默认支持,有些是默认关闭支持的,我们不妨来试一下它和上面的两个工具有什么不同,我们将上面的两个代码示例合并到一起施行,
golangci-lint help linters查看它都支持哪些静态检查工具:
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled bydefault
unused ✔ Enabled bydefault
...
// ... lots of output ...
$ golangci-lint help linters
// ... lots of output ...
staticcheck ✔ Enabled by default
gosimple ✔ Enabled by default
unused ✔ Enabled by default
...
// ... lots of output ...
$
golangci-lint..
总而言之 🎉🎊🙌大吉大利、 我懵了。 今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋总而言之 🎉🎊🙌大吉大利、今晚吃鸡🐓🍗🍖😋如下 📝💡:提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪提高我们的逼格;🚀💪能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;降低维护成本 💸;确保所有成员都遵循相同的编码规范 👥.能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;能够提升我们写出更加优雅的代码,为项目带来更高的质量 🚀;降低维护成本 💸;确保所有成员都遵循相同的编码规范 👥..totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;.totalCount++;可以发现''一些很秀的操作++;;);)~~ 😉~~ 😉~~ 😉~~ 😉~~ 😉~~ 😉~~ 😉有时候真的是 ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~od ~~o).byebye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻bye-bye~~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻再见了您嘞~~ 👋🏻下次再见喽~~~ 🐷下次再见喽~~~ 🐷下次再见喽~~~ 🐷下次再见喽~~~ 🐷下次再见喽~~~ 🐷拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰拜拜咯~~~~ 🐰Bye for now! 📠Bye for now! 📠Bye for now! 📠Bye for now! 📠Bye for now! 📠See you soon! 👀See you soon! 👀See you soon! 👀See you soon! 👀See you soon! 👀Later, folks! 👫Later, folks! 👫Later, folks! 👫Later, folks! 👫Later, folks!.byebye~~~ 👋🏻.byebye~~~ 👋🏻.byebye~~~ 👋🏻.byebye~~~

