Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

recvgroup

A golangci-lint module plugin that enforces method organisation by receiver type.

Rules

Contiguous grouping (always on)

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()   {}

Same file (same-file)

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 declaration

Installation

The linter is distributed as a golangci-lint module plugin. It requires a custom-built golangci-lint binary.

1. Add .custom-gcl.yml to your repo root

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 published

2. Build the custom binary

golangci-lint custom

This produces a custom-gcl binary in the current directory.

3. Enable in .golangci.yml

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

4. Run

./custom-gcl run ./...

Configuration

Option Type Default Description
same-file bool false Require all methods to be declared in the same file as their type definition

Development

cd recvgroup
go test ./...

Tests use analysistest against fixtures in testdata/src/.

About

A golangci-lint module plugin that enforces method organization by receiver type.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages