Skip to content

manuelarte/funcorder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧐 FuncOrder

Go Report Card version

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

About

Go Linter 🧐 that checks the order of methods and constructors

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •