A golangci-lint module plugin that enforces method organisation by receiver type.
All methods on the same receiver type must appear contiguously within a file. If methods of
type A appear, then methods of type B, then methods of A again — that is a violation.
Non-method functions interspersed between groups are ignored.
// bad
func (a *A) First() {}
func (b *B) Only() {}
func (a *A) Second() {} // recvgroup: methods of A are not grouped
// good
func (a *A) First() {}
func (a *A) Second() {}
func (b *B) Only() {}When enabled, every method must be declared in the same file as its receiver type definition.
// types.go
type Foo struct{}
// foo.go — violation when same-file is enabled
func (f *Foo) Do() {} // recvgroup: method on Foo must be in the same file as the type declarationThe linter is distributed as a golangci-lint module plugin. It requires a custom-built golangci-lint binary.
version: "v2.11.3" # match your installed golangci-lint version
plugins:
- module: "github.com/brahmlower/recvgroup"
import: "github.com/brahmlower/recvgroup/plugin"
path: "/path/to/recvgroup" # local path or a tagged version once publishedgolangci-lint customThis produces a custom-gcl binary in the current directory.
version: "2"
linters:
enable:
- recvgroup
settings:
custom:
recvgroup:
type: "module"
description: "checks that methods on the same receiver type are grouped contiguously within a file"
settings:
same-file: false # set to true to require methods in the same file as their type./custom-gcl run ./...| Option | Type | Default | Description |
|---|---|---|---|
same-file |
bool | false |
Require all methods to be declared in the same file as their type definition |
cd recvgroup
go test ./...Tests use analysistest against fixtures in testdata/src/.