Skip to content
/ gostyle Public

gostyle is a set of analyzers for coding styles.

License

Notifications You must be signed in to change notification settings

k1LoW/gostyle

Repository files navigation

gostyle

Go Reference build Coverage Code to Test Ratio Test Execution Time

gostyle is a set of analyzers for coding styles.

Disclaimer

gostyle IS NOT Go Style.

"Go Style" in Google Style Guides is a great style and we will actively refer to it, but we cannot implement the same rules perfectly, and we may extend the rules.

gostyle IS NOT STANDARD.

gostyle IS to help you maintain YOUR Go project coding STYLE.

Usage

$ go vet -vettool=`which gostyle`

On GitHub Actions

:octocat: GitHub Actions for gostyle is here !!

# .github/workflows/ci.yml
on:
  push:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
[...]
      -
        uses: k1LoW/gostyle-action@v1
        with:
          config-file: .gostyle.yml
[...]

Analyzers

Although not perfect, it provides analyzers based on helpful styles.

"Effective Go" by The Go Authors is licensed under CC BY 4.0

Go Style in Google Style Guides

"Google Style Guides" by Google is licensed under CC BY 3.0

Disabling and Ignoring

Disable analyzer

Use -[analyser name].disable flag.

$ go vet -vettool=`which gostyle` -mixedcaps.disable # Disable mixedcaps analyzer only

Ignore directive

  • //lint:ignore
  • //nolint:all
  • //nostyle:all
  • //nostyle:[analyzer name] (e.g. //nostyle:mixedcaps)

Configuration

gostyle can be configured by -gostyle.config=$PWD/.gostyle.yml like golangci-lint.

$ gostyle init
.gostyle.yml is generated
$ go vet -vettool=`which gostyle` -gostyle.config=$PWD/.gostyle.yml ./...
# All available settings of specific analyzers.
analyzers-settings:
  # See the dedicated "analyzers-settings" documentation section.
  option: value
analyzers:
  disable:
    # Disable specific analyzers.
    - analyzer-name

analyzers-settings:

contexts

analyzers-settings:
  contexts:
    include-generated: false # include generated codes (default: false)
    exclude-test: true       # exclude test files (default: false)

dontpanic

analyzers-settings:
  dontpanic:
    include-generated: false # include generated codes (default: false)
    exclude-test: true       # exclude test files (default: false)

errorstrings

analyzers-settings:
  errorstrings:
    include-generated: false # include generated codes (default: false)
    exclude-test: true       # exclude test files (default: false)

funcfmt

analyzers-settings:
  funcfmt:
    include-generated: false # include generated codes (default: false)

getters

analyzers-settings:
  getters:
    include-generated: false # include generated codes (default: false)
    exclude:                 # exclude words
      - GetViaHTTP

handlerrors

( NOT handleerrors )

analyzers-settings:
  handlerrors:
    include-generated: false # include generated codes (default: false)
    exclude-test: true       # exclude test files (default: false)

ifacenames

analyzers-settings:
  ifacenames:
    include-generated: false # include generated codes (default: false)
    all: true                # all interface names with the -er suffix are required (default: false)

mixedcaps

analyzers-settings:
  mixedcaps:
    include-generated: false # include generated codes (default: false)
    exclude:                 # exclude words
      - DBTX
      - EXPECT

nilslices

analyzers-settings:
  nilslices:
    include-generated: false # include generated codes (default: false)

pkgnames

analyzers-settings:
  pkgnames:
    include-generated: false # include generated codes (default: false)

recvnames

analyzers-settings:
  recvnames:
    include-generated: false # include generated codes (default: false)
    max: 3                   # max length of receiver name (default: 2)

recvtype

analyzers-settings:
  recvnames:
    include-generated: false # include generated codes (default: false)

repetition

analyzers-settings:
  repetition:
    include-generated: false # include generated codes (default: false)
    exclude:                 # exclude words
      - limitStr

typealiases

analyzers-settings:
  typealiases:
    include-generated: false # include generated codes (default: false)
    exclude:                 # exclude words
      - TmpAliasHeader

underscores

analyzers-settings:
  underscores:
    include-generated: false # include generated codes (default: false)
    exclude:                 # exclude words
      - DBTX
      - EXPECT

useany

analyzers-settings:
  useany:
    include-generated: false # include generated codes (default: false)

useq

analyzers-settings:
  useq:
    include-generated: false # include generated codes (default: false)

varnames

analyzers-settings:
  varnames:
    include-generated: false  # include generated codes (default: false)
    small-scope-max: 5        # max lines for small scope (default: 7)
    small-varname-max: 3      # max length of variable name for small scope (default: -1)
    medium-scope-max: 10      # max lines for medium scope (default: 15)
    medium-varname-max: 5     # max length of variable name for medium scope (default: -1)
    large-scope-max: 15       # max lines for large scope (default: 25)
    large-varname-max: 7      # max length of variable name for large scope (default: -1)
    very-large-varname-max: 9 # max length of variable name for very large scope (default: -1)
    exclude:                  # exclude words
      - hostname

Install

go install:

$ go install github.com/k1LoW/gostyle@latest

deb:

$ export GOSTYLE_VERSION=X.X.X
$ curl -o gostyle.deb -L https://github.com/k1LoW/gostyle/releases/download/v$GOSTYLE_VERSION/gostyle_$GOSTYLE_VERSION-1_amd64.deb
$ dpkg -i gostyle.deb

RPM:

$ export GOSTYLE_VERSION=X.X.X
$ yum install https://github.com/k1LoW/gostyle/releases/download/v$GOSTYLE_VERSION/gostyle_$GOSTYLE_VERSION-1_amd64.rpm

apk:

$ export GOSTYLE_VERSION=X.X.X
$ curl -o gostyle.apk -L https://github.com/k1LoW/gostyle/releases/download/v$GOSTYLE_VERSION/gostyle_$GOSTYLE_VERSION-1_amd64.apk
$ apk add gostyle.apk

homebrew tap:

$ brew install k1LoW/tap/gostyle

aqua:

$ aqua g -i k1LoW/gostyle

manually:

Download binary from releases page