函数应按粗略的调用顺序排序。
同一文件中的函数应按接收者分组。
因此,导出的函数应先出现在文件中,放在struct, const, var定义的后面。
在定义类型之后,但在接收者的其余方法之前,可能会出现一个 newXYZ()/NewXYZ()
由于函数是按接收者分组的,因此普通工具函数应在文件末尾出现。
Bad | Good |
---|---|
func (s *something) Cost() { return calcCost(s.weights) }
type something struct{ ... }
func calcCost(n []int) int {...}
func (s *something) Stop() {...}
func newSomething() *something { return &something{} } |
type something struct{ ... }
func newSomething() *something { return &something{} }
func (s *something) Cost() { return calcCost(s.weights) }
func (s *something) Stop() {...}
func calcCost(n []int) int {...} |