Skip to content

Uber Go style should clarify ordering for type declarations and methods in files with multiple types #146

Open
@andrewmains12

Description

I (and several other Uber engineers) are a bit confused by the https://github.com/uber-go/guide/blob/master/style.md#function-grouping-and-ordering section, specifically for the case where there are multiple types in one file.

Should it be:

// All type defs at top.

type something struct{ ... }
type somethingElse struct { ... }

func newSomething() *something {
    return &something{}
}

func (s *something) Cost() {
  return calcCost(s.weights)
}

func newSomethingElse() *somethingElse() {
  return &somethingElse{}
}
func (s *somethingElse) DoOtherThing() {

}

func calcCost(n []int) int {...}

or:

// Type defs near methods
type something struct{ ... }

func newSomething() *something {
    return &something{}
}

func (s *something) Cost() {
  return calcCost(s.weights)
}

type somethingElse struct { ... }


func newSomethingElse() *somethingElse() {
  return &somethingElse{}
}
func (s *somethingElse) DoOtherThing() {

}

func calcCost(n []int) int {...}

The style guide states:

Therefore, exported functions should appear first in a file, after struct, const, var definitions.

which makes me assume that "all type defs" at the top is correct, but it's not 100% clear from the example.

We should clarify this situation in the style guide (or, if the style guide has no opinion on it, state that as well).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions