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: trueconstructor: false# Checks if the exported methods of a structure are placed before the unexported ones.# Default: truestruct-method: false# Checks if the constructors and/or structure methods are sorted alphabetically.# Default: falsealphabetical: true# Checks that exported functions are placed before unexported functions.# Default: falsefunction: trueInstall FuncOrder linter using
go install github.com/manuelarte/funcorder@latestAnd then use it with
funcorder [-constructor=true|false] [-struct-method=true|false] [-alphabetical=true|false] [-function=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.function:true|false(defaultfalse) Checks that exported functions are placed before unexported functions.
This rule checks that the exported method are placed before the unexported ones, e.g:
| ❌ Bad | ✅ Good |
|---|---|
typeMyStructstruct {
Namestring
}
// ❌ unexported method // placed before exported methodfunc (mMyStruct) lenName() int { returnlen(m.Name)
}
func (mMyStruct) GetName() string {
returnm.Name
}
... | typeMyStructstruct {
Namestring
}
// ✅ exported methods before // unexported methodsfunc (mMyStruct) GetName() string {
returnm.Name
}
func (mMyStruct) lenName() int {
returnlen(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 declarationfuncNewMyStruct() MyStruct {
returnMyStruct{Name: "John"}
}
typeMyStructstruct {
Namestring
}
... | typeMyStructstruct {
Namestring
}
// ✅ `constructor "NewMyStruct" placed // after the struct declaration // and before the struct's methods`funcNewMyStruct() MyStruct {
returnMyStruct{Name: "John"}
}
// other MyStruct's methods... |
This rule checks:
Constructorfunctions are sorted alphabetically (ifconstructorsetting/parameter istrue).Methodsare sorted alphabetically (ifstruct-methodsetting/parameter istrue) for each group (exported and unexported).
| ❌ Bad | ✅ Good |
|---|---|
typeMyStructstruct {
Namestring
}
funcNewMyStruct() MyStruct {
returnMyStruct{Name: "John"}
}
// ❌ constructor "NewAMyStruct" should be placed // before "NewMyStruct"funcNewAMyStruct() MyStruct {
returnMyStruct{Name: "John"}
}
func (mMyStruct) GoodMorning() string {
return"good morning"
}
// ❌ method "GoodAfternoon" should be placed // before "GoodMorning"func (mMyStruct) GoodAfternoon() string {
return"good afternoon"
}
func (mMyStruct) hello() string {
return"hello"
}
// ❌ method "bye" should be placed // before "hello"func (mMyStruct) bye() string {
return"bye"
}
... | typeMyStructstruct {
Namestring
}
// ✅ constructors sorted alphabeticallyfuncNewAMyStruct() MyStruct {
returnMyStruct{Name: "John"}
}
funcNewMyStruct() MyStruct {
returnMyStruct{Name: "John"}
}
// ✅ exported methods sorted alphabeticallyfunc (mMyStruct) GoodAfternoon() string {
return"good afternoon"
}
func (mMyStruct) GoodMorning() string {
return"good morning"
}
// ✅ unexported methods sorted alphabeticallyfunc (mMyStruct) bye() string {
return"bye"
}
func (mMyStruct) hello() string {
return"hello"
}
... |
This rule checks that exported functions (those with no receiver) are placed before unexported ones within each file.
The init function is excluded from this rule.
| ❌ Bad | ✅ Good |
|---|---|
// ❌ unexported function placed// before exported functionfunchelper() string {
return"helper"
}
funcPublicFunc() string {
return"public"
} | // ✅ exported function placed// before unexported functionfuncPublicFunc() string {
return"public"
}
funchelper() string {
return"helper"
} |
- Following Uber Style Guidelines about function-grouping-and-ordering