1# This code is licensed under the terms of the MIT license.
  2
  3## Golden config for golangci-lint v1.54
  4#
  5# This is the best config for golangci-lint based on my experience and opinion.
  6# It is very strict, but not extremely strict.
  7# Feel free to adopt and change it for your needs.
  8#
  9# @neilotoole: ^^ Well, it's less strict now!
 10# Based on: https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322
 11
 12run:
 13  # Timeout for analysis, e.g. 30s, 5m.
 14  # Default: 1m
 15  timeout: 5m
 16
 17  tests: false
 18
 19  skip-dirs:
 20    - scratch
 21
 22
 23
 24
 25
 26output:
 27  sort-results: true
 28
 29# This file contains only configs which differ from defaults.
 30# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
 31linters-settings:
 32  cyclop:
 33    # The maximal code complexity to report.
 34    # Default: 10
 35    max-complexity: 50
 36    # The maximal average package complexity.
 37    # If it's higher than 0.0 (float) the check is enabled
 38    # Default: 0.0
 39    package-average: 10.0
 40
 41  errcheck:
 42    # Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
 43    # Such cases aren't reported by default.
 44    # Default: false
 45    check-type-assertions: true
 46
 47  exhaustive:
 48    # Program elements to check for exhaustiveness.
 49    # Default: [ switch ]
 50    check:
 51      - switch
 52      - map
 53
 54  funlen:
 55    # Checks the number of lines in a function.
 56    # If lower than 0, disable the check.
 57    # Default: 60
 58    lines: 150
 59    # Checks the number of statements in a function.
 60    # If lower than 0, disable the check.
 61    # Default: 40
 62    statements: 100
 63
 64  gocognit:
 65    # Minimal code complexity to report
 66    # Default: 30 (but we recommend 10-20)
 67    min-complexity: 50
 68
 69  gocritic:
 70    # Settings passed to gocritic.
 71    # The settings key is the name of a supported gocritic checker.
 72    # The list of supported checkers can be find in https://go-critic.github.io/overview.
 73    settings:
 74      captLocal:
 75        # Whether to restrict checker to params only.
 76        # Default: true
 77        paramsOnly: false
 78      underef:
 79        # Whether to skip (*x).method() calls where x is a pointer receiver.
 80        # Default: true
 81        skipRecvDeref: false
 82
 83  gocyclo:
 84    # Minimal code complexity to report.
 85    # Default: 30 (but we recommend 10-20)
 86    min-complexity: 50
 87
 88  gofumpt:
 89    # Module path which contains the source code being formatted.
 90    # Default: ""
 91    module-path: github.com/neilotoole/jsoncolor
 92    # Choose whether to use the extra rules.
 93    # Default: false
 94    extra-rules: true
 95
 96  gomnd:
 97    # List of function patterns to exclude from analysis.
 98    # Values always ignored: `time.Date`,
 99    # `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`,
100    # `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`.
101    # Default: []
102    ignored-functions:
103      - make
104      - os.Chmod
105      - os.Mkdir
106      - os.MkdirAll
107      - os.OpenFile
108      - os.WriteFile
109      - prometheus.ExponentialBuckets
110      - prometheus.ExponentialBucketsRange
111      - prometheus.LinearBuckets
112    ignored-numbers:
113      - "2"
114      - "3"
115
116  gomodguard:
117    blocked:
118      # List of blocked modules.
119      # Default: []
120      modules:
121        - github.com/golang/protobuf:
122            recommendations:
123              - google.golang.org/protobuf
124            reason: "see https://developers.google.com/protocol-buffers/docs/reference/go/faq#modules"
125        - github.com/satori/go.uuid:
126            recommendations:
127              - github.com/google/uuid
128            reason: "satori's package is not maintained"
129        - github.com/gofrs/uuid:
130            recommendations:
131              - github.com/google/uuid
132            reason: "gofrs' package is not go module"
133
134  govet:
135    # Enable all analyzers.
136    # Default: false
137    enable-all: true
138    # Disable analyzers by name.
139    # Run `go tool vet help` to see all analyzers.
140    # Default: []
141    disable:
142      - fieldalignment # too strict
143    # Settings per analyzer.
144    settings:
145      shadow:
146        # Whether to be strict about shadowing; can be noisy.
147        # Default: false
148        strict: false
149
150  lll:
151    # Max line length, lines longer will be reported.
152    # '\t' is counted as 1 character by default, and can be changed with the tab-width option.
153    # Default: 120.
154    line-length: 120
155    # Tab width in spaces.
156    # Default: 1
157    tab-width: 1
158
159  nakedret:
160    # Make an issue if func has more lines of code than this setting, and it has naked returns.
161    # Default: 30
162    max-func-lines: 0
163
164  nestif:
165    # Minimal complexity of if statements to report.
166    # Default: 5
167    min-complexity: 20
168
169  nolintlint:
170    # Exclude following linters from requiring an explanation.
171    # Default: []
172    allow-no-explanation: [ funlen, gocognit, lll ]
173    # Enable to require an explanation of nonzero length after each nolint directive.
174    # Default: false
175    require-explanation: false
176    # Enable to require nolint directives to mention the specific linter being suppressed.
177    # Default: false
178    require-specific: true
179
180  rowserrcheck:
181    # database/sql is always checked
182    # Default: []
183    packages:
184#      - github.com/jmoiron/sqlx
185
186  tenv:
187    # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
188    # Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
189    # Default: false
190    all: true
191
192
193linters:
194  disable-all: true
195
196  enable:
197    ## enabled by default
198    - errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases
199    - gosimple # specializes in simplifying a code
200    - govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
201    - ineffassign # detects when assignments to existing variables are not used
202    - staticcheck # is a go vet on steroids, applying a ton of static analysis checks
203    - typecheck # like the front-end of a Go compiler, parses and type-checks Go code
204    - unused # checks for unused constants, variables, functions and types
205
206
207    #    ## disabled by default
208    - asasalint # checks for pass []any as any in variadic func(...any)
209    - asciicheck # checks that your code does not contain non-ASCII identifiers
210    - bidichk # checks for dangerous unicode character sequences
211    - bodyclose # checks whether HTTP response body is closed successfully
212    - cyclop # checks function and package cyclomatic complexity
213    - dupl # tool for code clone detection
214    - durationcheck # checks for two durations multiplied together
215    - errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
216    - errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
217    - execinquery # checks query string in Query function which reads your Go src files and warning it finds
218    - exhaustive # checks exhaustiveness of enum switch statements
219    - exportloopref # checks for pointers to enclosing loop variables
220    - forbidigo # forbids identifiers
221    - funlen # tool for detection of long functions
222    - gochecknoinits # checks that no init functions are present in Go code
223    - gocognit # computes and checks the cognitive complexity of functions
224    - goconst # finds repeated strings that could be replaced by a constant
225    - gocritic # provides diagnostics that check for bugs, performance and style issues
226    - gocyclo # computes and checks the cyclomatic complexity of functions
227    - godot # checks if comments end in a period
228    - gofumpt
229    - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
230    #    - gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
231    - gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
232    - goprintffuncname # checks that printf-like functions are named with f at the end
233    - gosec # inspects source code for security problems
234    - lll # reports long lines
235    - loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
236    - makezero # finds slice declarations with non-zero initial length
237    - nakedret # finds naked returns in functions greater than a specified function length
238    - nestif # reports deeply nested if statements
239    - nilerr # finds the code that returns nil even if it checks that the error is not nil
240    - nilnil # checks that there is no simultaneous return of nil error and an invalid value
241    - noctx # finds sending http request without context.Context
242    - nolintlint # reports ill-formed or insufficient nolint directives
243    - nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
244    - predeclared # finds code that shadows one of Go's predeclared identifiers
245    - promlinter # checks Prometheus metrics naming via promlint
246    - reassign # checks that package variables are not reassigned
247    - revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
248    - stylecheck # is a replacement for golint
249    - tenv # detects using os.Setenv instead of t.Setenv since Go1.17
250    - testableexamples # checks if examples are testable (have an expected output)
251    - tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
252    - unconvert # removes unnecessary type conversions
253    - unparam # reports unused function parameters
254    - usestdlibvars # detects the possibility to use variables/constants from the Go standard library
255    - whitespace # detects leading and trailing whitespace
256
257    ## These three linters are disabled for now due to generics: https://github.com/golangci/golangci-lint/issues/2649
258    #- rowserrcheck # checks whether Err of rows is checked successfully  # Disabled because:  https://github.com/golangci/golangci-lint/issues/2649
259    #- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
260    #- wastedassign # finds wasted assignment statements
261
262
263    ## you may want to enable
264    #- decorder # checks declaration order and count of types, constants, variables and functions
265    #- exhaustruct # checks if all structure fields are initialized
266    #- gochecknoglobals # checks that no global variables exist
267    #- godox # detects FIXME, TODO and other comment keywords
268    #- goheader # checks is file header matches to pattern
269    #- gomnd # detects magic numbers
270    #- interfacebloat # checks the number of methods inside an interface
271    #- ireturn # accept interfaces, return concrete types
272    #- prealloc # [premature optimization, but can be used in some cases] finds slice declarations that could potentially be preallocated
273    #- varnamelen # [great idea, but too many false positives] checks that the length of a variable's name matches its scope
274    #- wrapcheck # checks that errors returned from external packages are wrapped
275
276    ## disabled
277    #- containedctx # detects struct contained context.Context field
278    #- contextcheck # [too many false positives] checks the function whether use a non-inherited context
279    #- depguard # [replaced by gomodguard] checks if package imports are in a list of acceptable packages
280    #- dogsled # checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
281    #- dupword # [useless without config] checks for duplicate words in the source code
282    #- errchkjson # [don't see profit + I'm against of omitting errors like in the first example https://github.com/breml/errchkjson] checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted
283    #- forcetypeassert # [replaced by errcheck] finds forced type assertions
284    #- goerr113 # [too strict] checks the errors handling expressions
285    #- gofmt # [replaced by goimports] checks whether code was gofmt-ed
286    #- gofumpt # [replaced by goimports, gofumports is not available yet] checks whether code was gofumpt-ed
287    #- grouper # analyzes expression groups
288    #- importas # enforces consistent import aliases
289    #- maintidx # measures the maintainability index of each function
290    #- misspell # [useless] finds commonly misspelled English words in comments
291    #- nlreturn # [too strict and mostly code is not more readable] checks for a new line before return and branch statements to increase code clarity
292    #- paralleltest # [too many false positives] detects missing usage of t.Parallel() method in your Go test
293    #- tagliatelle # checks the struct tags
294    #- thelper # detects golang test helpers without t.Helper() call and checks the consistency of test helpers
295    #- wsl # [too strict and mostly code is not more readable] whitespace linter forces you to use empty lines
296
297    ## deprecated
298    #- deadcode # [deprecated, replaced by unused] finds unused code
299    #- exhaustivestruct # [deprecated, replaced by exhaustruct] checks if all struct's fields are initialized
300    #- golint # [deprecated, replaced by revive] golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes
301    #- ifshort # [deprecated] checks that your code uses short syntax for if-statements whenever possible
302    #- interfacer # [deprecated] suggests narrower interface types
303    #- maligned # [deprecated, replaced by govet fieldalignment] detects Go structs that would take less memory if their fields were sorted
304    #- nosnakecase # [deprecated, replaced by revive var-naming] detects snake case of variable naming and function name
305    #- scopelint # [deprecated, replaced by exportloopref] checks for unpinned variables in go programs
306    #- structcheck # [deprecated, replaced by unused] finds unused struct fields
307    #- varcheck # [deprecated, replaced by unused] finds unused global variables and constants
308
309
310issues:
311  # Maximum count of issues with the same text.
312  # Set to 0 to disable.
313  # Default: 3
314  max-same-issues: 3
315
316  exclude-rules:
317    - source: "^//\\s*go:generate\\s"
318      linters: [ lll ]
319    - source: "(noinspection|TODO)"
320      linters: [ godot ]
321    - source: "//noinspection"
322      linters: [ gocritic ]
323    - source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {"
324      linters: [ errorlint ]
325    - path: "_test\\.go"
326      linters:
327        - bodyclose
328        - dupl
329        - funlen
330        - goconst
331        - gosec
332        - noctx
333        - wrapcheck
334