6.测试静态代码另外一个好的做法是使用linters 分析来做静态代码测试 。为此,我们可以使用golang-ci工具(Go里的快速linter,比gometaliner好) 。
还是因为有了Makefile,你只需要获取此处我列出的工具,例如golang-cli:
$ make get.tools
一个好的做法是新建一个.golangci.yml文件来定义我们想要的linter配置 。下面是golang-cli配置示例:
run:modules-download-mode: vendordeadline: 10missues-exit-code: 1tests: trueskip-files:- ".*\.pb\.go$"- ".*\.gen\.go$"- "mock_.*\.go"linters:enable:- govet # check standard vet rules- golint # check standard linting rules- staticcheck# comprehensive checks- errcheck # find unchecked errors- ineffassign# find ineffective assignments- varcheck # find unused global variables and constants- structcheck# check for unused struct parameters- deadcode # find code that is not used- nakedret # check for naked returns- goimports # fix import order- misspell # check spelling- unconvert # remove unnecessary conversions- maligned # check for better memory usagedisable:- goconst # check for things that could be replaced by constants- gocyclo # needs tweaking- depguard # unused- gosec # needs tweaking- dupl # slow- interfacer # not that useful- gosimple # part of staticcheck- unused # part of staticcheck- megacheck # part of staticcheck- lllfast: falseoutput:format: colored-line-numberprint-issued-lines: trueprint-linter-name: truelinters-settings:errcheck:# report about not checking of errors in type assetions: `a := b.(MyStruct)`;# default is false: such cases aren't reported by default.check-type-assertions: false# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;# default is false: such cases aren't reported by default.check-blank: falsegovet:# report about shadowed variables#TODO# check-shadowing: true# Obtain type information from installed (to $GOPATH/pkg) package files:# golangci-lint will execute `go install -i` and `go test -i` for analyzed packages# before analyzing them.# Enable this option only if all conditions are met:# 1. you use only "fast" linters (--fast e.g.): no program loading occurs# 2. you use go >= 1.10# 3. you do repeated runs (false for CI) or cache $GOPATH/pkg or `go env GOCACHE` dir in CI.use-installed-packages: falsegolint:min-confidence: 0.8gofmt:simplify: truegocyclo:min-complexity: 10maligned:suggest-new: truedupl:threshold: 150goconst:min-len: 3min-occurrences: 3misspell:locale: USlll:line-length: 140tab-width: 1unused:# treat code as a program (not a library) and report unused exported identifiers; default is false.# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:# if it's called for subdir of a project it can't find funcs usages. All text editor integrations# with golangci-lint call it on a directory with the changed file.check-exported: falseunparam:# call graph construction algorithm (cha, rta). In general, use cha for libraries,# and rta for programs with main packages. Default is cha.algo: cha# Inspect exported functions, default is false. Set to true if no external program/library imports your code.# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:# if it's called for subdir of a project it can't find external interfaces. All text editor integrations# with golangci-lint call it on a directory with the changed file.check-exported: falsenakedret:# make an issue if func has more lines of code than this setting and it has naked returns; default is 30max-func-lines: 30prealloc:# Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.# True by default.simple: truerange-loops: true# Report preallocation suggestions on range loops, true by defaultfor-loops: false# Report preallocation suggestions on for loops, false by defaultissues:max-per-linter: 0max-same: 0new: falseexclude-use-default: false
接下来我们可以检查代码是否包含lint错误:
$ make lint.full==> linters (slow)INFO [config_reader] Config search paths: [./ /Users/uidn3817/git/github.com/scraly/http-go-server/internal /Users/uidn3817/git/github.com/scraly/http-go-server /Users/uidn3817/git/github.com/scraly /Users/uidn3817/git/github.com /Users/uidn3817/git /Users/uidn3817 /Users /]INFO [config_reader] Used config file .golangci.ymlINFO [lintersdb] Active 13 linters: [deadcode errcheck goimports golint govet ineffassign maligned misspell nakedret structcheck typecheck unconvert varcheck]INFO [loader] Go packages loading at mode load types and syntax took 1.403040989sINFO [loader] SSA repr building timing: packages building 17.446103ms, total 215.11635msINFO [runner] worker.1 took 319.338µs with stages: unconvert: 126.709µs, structcheck: 105.706µs, varcheck: 80.624µsINFO [runner] worker.8 took 279.76µs with stages: errcheck: 102.203µs, nakedret: 88.6µs, deadcode: 54.547µs, maligned: 22.796µs, typecheck: 2.416µsINFO [runner] worker.2 took 908nsINFO [runner] worker.7 took 1.424891ms with stages: ineffassign: 1.419068msINFO [runner] worker.4 took 2.395856ms with stages: goimports: 2.39105msINFO [runner] worker.6 took 75.843872ms with stages: golint: 75.832987msINFO [runner] worker.5 took 77.126536ms with stages: misspell: 77.092913msINFO [runner] worker.3 took 124.506172ms with stages: govet: 124.498137msINFO [runner] Workers idle times: #1: 124.053298ms, #2: 123.973576ms, #4: 122.078696ms, #5: 47.339761ms, #6: 48.693713ms, #7: 122.946009ms, #8: 124.035904msINFO [runner] processing took 19.597µs with stages: max_same_issues: 16.123µs, cgo: 541ns, skip_dirs: 493ns, nolint: 398ns, max_from_linter: 280ns, path_prettifier: 179ns, filename_unadjuster: 172ns, replacement_builder: 170ns, autogenerated_exclude: 170ns, exclude: 164ns, diff: 162ns, skip_files: 161ns, identifier_marker: 150ns, source_code: 97ns, path_shortener: 97ns, max_per_file_from_linter: 82ns, exclude-rules: 80ns, uniq_by_line: 78nsINFO File cache stats: 0 entries of total size 0BINFO Memory: 23 samples, avg is 255.8MB, max is 672.0MBINFO Execution took 2.119246564s
推荐阅读
- SEO中,H1、H2、H3标签应该如何使用?
- 如何添加 Photoshop 的各类工具
- 腰骶神经根损伤如何治疗
- 腰麻后神经损伤如何治疗
- 扁导体发炎了怎么办
- 屁股为什么会变大
- 如何判断结膜炎的类型
- 淘宝开店如何装修 淘宝店铺不装修可以吗
- 驻马店,茶园建设如火如荼 茶业发展正逢时
- 皎然,皎如明月 灿然似星