Skip to content

Project code checks

Vojtech Mašek edited this page Aug 19, 2019 · 2 revisions

How to enable Golang code checks during builds

This guide describes the use of the mgechev/revive project.

When you enable this revive, every build will fail unless it meets the requirements defined by rules in a configuration file. This is a strained way of how to enforce proper code style.

Since revive lints the individual files in separate goroutines, it outperforms golint about 4 times. Also, revive can be extended by custom rules.

Enabling checks

To enable checks during a build, two additional lines are required in Dockerfile:

RUN go get -u github.com/mgechev/revive
RUN revive -formatter stylish -config revive-config.toml -exclude ./vendor/... ./...

The first command downloads the package via go get. The second command invokes the check:

  • -formatter stylish formats the output message to the most suitable form for a build log. The list of available formatter can be found here.
  • -config revive-config.toml passes the configuration file to revive. The content of the configuration file will be described below.
  • -exclude ./vendor/... excludes the vendor directory from checks.
  • ./... will check every file in a project.

Configuration file

This is an example of recommended configuration:

# When set to false, ignores files with "GENERATED" header, similar to golint
ignoreGeneratedHeader = true

# Sets the default severity to "warning"
severity = "warning"

# Sets the default failure confidence. This means that linting errors
# with less than 0.8 confidence will be ignored.
confidence = 0.8

# Sets the error code for failures with severity "error"
errorCode = 10

# Sets the error code for failures with severity "warning"
warningCode = 11

[rule.blank-imports]
[rule.context-as-argument]
[rule.context-keys-type]
[rule.dot-imports]
[rule.error-return]
[rule.error-strings]
[rule.error-naming]
[rule.exported]
[rule.if-return]
[rule.increment-decrement]
[rule.var-naming]
[rule.var-declaration]
[rule.package-comments]
[rule.range]
[rule.receiver-naming]
[rule.time-naming]
[rule.unexported-return]
[rule.indent-error-flow]
[rule.errorf]
[rule.empty-block]
[rule.superfluous-else]
[rule.unused-parameter]
[rule.unreachable-code]

Description of used configuration can be found here.

The list of available rules with description can be found here here.

Clone this wiki locally