gonaming-conventions

What are conventions for filenames in Go?


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.


Solution

  • 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