去样式指南? [关闭]

I'm starting in the programming with Go and I want to ask if there are some patterns to follow when programming, like:

// Package

// Structs orderer by importance

// Struct methods

// Un-exported methods

// Exported methods

// getters and setters

There is no specific guideline for this and even stdlib packages does not follow the same way. But as a rule of thumb you should:

  • Put exported, global constants and variables (like errors) near the top.
  • Keep structs together with their methods rather than grouping structs together and then their methods separately.
  • Group logical parts together (whenever structs, their methods, package functions (exported or not)). It will make easier splitting your package into multiple files later on. It makes reading easier as well as you don't have to jump all over the file.
  • Last, but not least: if you generate documentation from your package with godoc and can read it from top to bottom with understanding, then your order is probably correct :)

When in doubt, check some popular stdlib packages, like https://golang.org/src/net/http/server.go.

Other useful code style guidelines can be found here:

The two sources of good style in go are these two documents:

I would also recommend to use these tools for your code base: