What are conventions for filenames in Go?

david picture david · Aug 6, 2014 · Viewed 66.6k times · Source

I could find the conventions for naming packages in Go: no underscore between words, everything lowercase.

Does this convention apply to the filenames too?

Do you also put one struct in one file as if you did for a java class and then name the file after the struct?

Currently, if I have a struct WebServer, I put it in a file web_server.go.

Answer

JimB picture JimB · Aug 6, 2014

There's a few guidelines to follow.

  1. File names that begin with "." or "_" are ignored by the go tool
  2. Files with the suffix _test.go are only compiled and run by the go test tool.
  3. Files with os and architecture specific suffixes automatically follow those same constraints, e.g. name_linux.go will only build on linux, name_amd64.go will only build on amd64. This is the same as having a //+build amd64 line at the top of the file

See the docs for the go build tool for more details: https://golang.org/pkg/go/build/