Go Linter to check Functions/Methods Order.
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
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
(defaulttrue
) Checks that constructors are placed after the structure declaration.struct-method
:true|false
(defaulttrue
) Checks if the exported methods of a structure are placed before the unexported ones.alphabetical
:true|false
(defaultfalse
) Checks if the constructors and/or structure methods are sorted alphabetically.
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)
}
... |
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
... |
This rule checks:
Constructor
functions are sorted alphabetically (ifconstructor
setting/parameter istrue
).Methods
are sorted alphabetically (ifstruct-method
setting/parameter istrue
) 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"
}
... |
- Following Uber Style Guidelines about function-grouping-and-ordering