π§ FuncOrder

Go Linter to check Functions/Methods Order.
β¬οΈ Getting Started
As a golangci-lint linter
Define the rules in your golangci-lint configuration file, e.g:
linters:
enable:
- funcorder
...
settings:
funcorder:
# Checks that constructors are placed after the structure declaration.
# Default: true
constructor: false
# Checks if the exported methods of a structure are placed before the unexported ones.
# Default: true
struct-method: false
# Checks if the constructors and/or structure methods are sorted alphabetically.
# Default: false
alphabetical: true
Standalone application
Install FuncOrder linter using
go install github.com/manuelarte/funcorder@latest
And then use it with
funcorder [-constructor=true|false] [-struct-method=true|false] [-alphabetical=true|false] ./...
Parameters:
constructor: true|false (default true) Checks that constructors are placed after the structure declaration.
struct-method: true|false (default true) Checks if the exported methods of a structure are placed before the unexported ones.
alphabetical: true|false (default false) Checks if the constructors and/or structure methods are sorted alphabetically.
π Features
Check exported methods are placed before unexported methods
This rule checks that the exported method are placed before the unexported ones, e.g:
| β Bad | β
Good |
type MyStruct struct {
Name string
}
// β unexported method
// placed before exported method
func (m MyStruct) lenName() int {
return len(m.Name)
}
func (m MyStruct) GetName() string {
return m.Name
}
...
|
type MyStruct struct {
Name string
}
// β
exported methods before
// unexported methods
func (m MyStruct) GetName() string {
return m.Name
}
func (m MyStruct) lenName() int {
return len(m.Name)
}
...
|
Check Constructors functions are placed after struct declaration
This rule checks that the Constructor functions are placed after the struct declaration and before the struct's methods.
Constructor function
This linter considers a Constructor function a function that has the prefix New, or Must, and returns 1 or 2 types.
Where the 1st return type is a struct declared in the same file.
| β Bad | β
Good |
// β constructor "NewMyStruct" placed
// before the struct declaration
func NewMyStruct() MyStruct {
return MyStruct{Name: "John"}
}
type MyStruct struct {
Name string
}
...
|
type MyStruct struct {
Name string
}
// β
`constructor "NewMyStruct" placed
// after the struct declaration
// and before the struct's methods`
func NewMyStruct() MyStruct {
return MyStruct{Name: "John"}
}
// other MyStruct's methods
...
|
Check Constructors/Methods are sorted alphabetically
This rule checks:
Constructor functions are sorted alphabetically (if constructor setting/parameter is true).
Methods are sorted alphabetically (if struct-method setting/parameter is true) for each group (exported and unexported).
| β Bad | β
Good |
type MyStruct struct {
Name string
}
func NewMyStruct() MyStruct {
return MyStruct{Name: "John"}
}
// β constructor "NewAMyStruct" should be placed
// before "NewMyStruct"
func NewAMyStruct() MyStruct {
return MyStruct{Name: "John"}
}
func (m MyStruct) GoodMorning() string {
return "good morning"
}
// β method "GoodAfternoon" should be placed
// before "GoodMorning"
func (m MyStruct) GoodAfternoon() string {
return "good afternoon"
}
func (m MyStruct) hello() string {
return "hello"
}
// β method "bye" should be placed
// before "hello"
func (m MyStruct) bye() string {
return "bye"
}
...
|
type MyStruct struct {
Name string
}
// β
constructors sorted alphabetically
func NewAMyStruct() MyStruct {
return MyStruct{Name: "John"}
}
func NewMyStruct() MyStruct {
return MyStruct{Name: "John"}
}
// β
exported methods sorted alphabetically
func (m MyStruct) GoodAfternoon() string {
return "good afternoon"
}
func (m MyStruct) GoodMorning() string {
return "good morning"
}
// β
unexported methods sorted alphabetically
func (m MyStruct) bye() string {
return "bye"
}
func (m MyStruct) hello() string {
return "hello"
}
...
|
Resources