What are conventions for filenames in Go?

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 go docs for more details: https://pkg.go.dev/cmd/go


Go is quite liberal in terms of how you organise your code within a package, usually it's whatever improves readability and understanding of your code. The best way to learn how this is done is to study the masters, i.e. have a browse though the standard library:

http://golang.org/src/pkg/

There are 2 rules I can think of however. When specifying code to be compiled for different platforms, you use the platform name as a suffix:

mypkg_linux.go         // only builds on linux systems
mypkg_windows_amd64.go // only builds on windows 64bit platforms

Also if you have a file called server.go, the tests for that file would be in server_test.go.


In addition to the answer provided by JimB, regular file names are lower case, short, and without any sort of underscore or space. Generally, file names follow the same convention as package names. See the Package Names section of Effective Go.

See the strconv package for a good example.