Skip to content

Instantly share code, notes, and snippets.

@simonfontana
Created September 29, 2023 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonfontana/acacc595fbbdd8bb39481ba72e9f834b to your computer and use it in GitHub Desktop.
Save simonfontana/acacc595fbbdd8bb39481ba72e9f834b to your computer and use it in GitHub Desktop.
golangci example
# config file documentation: https://golangci-lint.run/usage/configuration/
run:
concurrency: 4
tests: true
timeout: 1m
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate
format: colored-line-number
# print lines of code with issue, default is true
print-issued-lines: true
# print linter name in the end of issue text, default is true
print-linter-name: true
linters-settings:
dupl:
# tokens count to trigger issue, 150 by default
threshold: 100
funlen:
# minimum length of function
lines: 60
# minimum number of statements in function
statements: 20
gci:
# Section configuration to compare against.
# Section names are case-insensitive and may contain parameters in ().
# The default order of sections is `standard > default > custom > blank > dot`,
# If `custom-order` is `true`, it follows the order of `sections` option.
# Default: ["standard", "default"]
sections:
- standard # Standard section: captures all standard packages.
- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(example.com) # Custom section: groups all imports with the specified Prefix.
# Enable custom order of sections.
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: true
gocognit:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 14
depguard:
rules:
main:
deny:
- pkg: github.com/stretchr/testify/assert
desc: "Use github.com/stretchr/testify/require instead"
importas:
alias:
- pkg: k8s.io/api/core/v1
alias: corev1
- pkg: k8s.io/api/apps/v1
alias: appsv1
- pkg: k8s.io/api/networking/v1
alias: networkingv1
- pkg: k8s.io/api/policy/v1
alias: policyv1
- pkg: k8s.io/api/rbac/v1
alias: rbacv1
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: metav1
- pkg: k8s.io/apimachinery/pkg/api/errors
alias: k8serr
# assert continues the test execution when a test fails.
# require implements the same assertions as assert but stops test execution when a test fails.
# assert is the better name but require has the better functionality, so require is now assert.
- pkg: github.com/stretchr/testify/require
alias: assert
no-unaliased: true
nestif:
# minimal complexity of if statements to report, 5 by default
min-complexity: 4
goconst:
# minimal length of string constant, 3 by default
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 2
# exclude tests from the search
ignore-tests: true
gocritic:
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks.
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
enabled-tags:
- diagnostic
- performance
- style
# Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
# disabled-checks:
goheader:
template: |-
Copyright (c) 2023 Example Company.
All rights reserved...
gomnd:
settings:
mnd:
# the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
checks: [argument,assign,case,condition,operation,return]
govet:
# report about shadowed variables
check-shadowing: true
lll:
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
line-length: 99
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
# ignore-words:
# - someword
nolintlint:
# Enable to ensure that nolint directives are all used. Default is true.
allow-unused: true
# Disable to ensure that nolint directives don't have a leading space. Default is true.
allow-leading-space: false
# Exclude following linters from requiring an explanation. Default is [].
allow-no-explanation: []
# Enable to require an explanation of nonzero length after each nolint directive. Default is false.
require-explanation: true
# Enable to require nolint directives to mention the specific linter being suppressed. Default is false.
require-specific: true
linters:
# Preferred behavior is to disable all linters and then manually enable the
# linters you want so linting does not break when an update adds a new linter.
disable-all: true
enable:
- asasalint # checks for pass []any as any in variadic func(...any)
- asciicheck # checks that your code does not contain non-ASCII identifiers
- bidichk # checks for dangerous unicode character sequences
- bodyclose # checks whether HTTP response body is closed successfully
- dupl # tool for code clone detection
- durationcheck # checks for two durations multiplied together
- depguard # allow and block list linter for direct Go module dependencies
- errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
- exhaustive # checks exhaustiveness of enum switch statements
- exportloopref # checks for pointers to enclosing loop variables
- forbidigo # forbids identifiers
- funlen # tool for detection of long functions
- gci # gci controls golang package import order and makes it always deterministic
- gochecknoglobals # checks that no global variables exist
- gochecknoinits # checks that no init functions are present in Go code
- gocognit # computes and checks the cognitive complexity of functions
- goconst # finds repeated strings that could be replaced by a constant
- gocritic # provides diagnostics that check for bugs, performance and style issues
- godot # checks if comments end in a period
- goerr113 # golang linter to check the errors handling expressions
- gofumpt # gofumpt checks whether code was gofumpt-ed
- goheader # checks is file header matches to pattern
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
- gomnd # detects magic numbers
- goprintffuncname # checks that printf-like functions are named with f at the end
- gosec # inspects source code for security problems
- gosimple # specializes in simplifying a code
- govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- importas
- ineffassign # detects when assignments to existing variables are not used
- lll # reports long lines
- maintidx # measures the maintainability index of each function
- makezero # finds slice declarations with non-zero initial length
- misspell # finds commonly misspelled English words in comments
- nakedret # finds naked returns in functions greater than a specified function length
- nestif # reports deeply nested if statements
- nilerr # finds the code that returns nil even if it checks that the error is not nil
- nilnil # checks that there is no simultaneous return of nil error and an invalid value
- noctx # finds sending http request without context.Context
- nolintlint # reports ill-formed or insufficient nolint directives
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
- prealloc # finds slice declarations that could potentially be pre-allocated
- predeclared # finds code that shadows one of Go's predeclared identifiers
- reassign # checks that package variables are not reassigned
- revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks
- stylecheck # is a replacement for golint
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
- testpackage # makes you use a separate _test package
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- typecheck # like the front-end of a Go compiler, parses and type-checks Go code
- unconvert # removes unnecessary type conversions
- unparam # reports unused function parameters
- unused # checks for unused constants, variables, functions and types
- usestdlibvars # detects the possibility to use variables/constants from the Go standard library
- whitespace # detects leading and trailing whitespace
- wrapcheck # checks that errors returned from external packages are wrapped
issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
# excluded by default patterns execute `golangci-lint run --help`
# exclude:
# - abcdef
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Ignore line length of build comments starting with "//+"
- linters:
- lll
source: "^//\\+"
# deepcopy autogenerated files are not detected by typecheck
- path: '.*generated.*'
linters:
- typecheck
# long and complex test functions are okay for tests
- linters:
- funlen
- maintidx
- gocognit
source: "^func Test"
# creating dynamic errors in tests is fine
- path: '.*_test.go'
linters:
- goerr113
- errcheck
- wrapcheck
# many of the test cases are similar, but the arguments and expected result differs
- path: '.*/.*_test.go'
linters:
- dupl
# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
# excluded by default patterns execute `golangci-lint run --help`.
# Default value for this option is true.
exclude-use-default: true
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 20
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 3
# Show only new issues: if there are unstaged changes or untracked files,
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
# It's a super-useful option for integration of golangci-lint into existing
# large codebase. It's not practical to fix all existing issues at the moment
# of integration: much better don't allow issues in new code.
# For CI setups, prefer --new-from-rev=HEAD~, as --new can skip linting the
# current patch if any scripts generate unstaged files before golangci-lint
# runs.
# new: true
# Show only new issues created after git revision `HEAD~`
# new-from-rev: HEAD~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment