199

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.

0

6 Answers 6

208

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

7
  • 3
    @AbhijeetRastogi: golang.org/pkg/go/build and golang.org/cmd/go
    – JimB
    Commented Nov 23, 2015 at 13:50
  • 6
    What I should to do if I want to build for unix and others. E.g. I can make two files file_windows.go and file_others.go. It works fine. But for file_unix.go and file_others.go it does'n' work. I don't want to create eight files darwin freebsg linux openbsd netbsd dragonfly solaris android.
    – Ivan Black
    Commented Dec 25, 2015 at 8:47
  • 1
    @Jimb What about camelCase ?
    – Fire
    Commented Dec 31, 2015 at 5:52
  • 6
    @Fire: filenames are generally all lowercase in case, both for consistency and for systems with case-insensitive filesystems.
    – JimB
    Commented Dec 31, 2015 at 13:50
  • 1
    For anyone with the same question as @IvanBlack, this can be accomplished using build tags. See here for a nice overview of how to do that: dave.cheney.net/2013/10/12/… Commented Nov 2, 2018 at 18:37
64

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.

2
  • 13
    what'd you give long files a name? mycommandsub1command.go or my_command_sub1command.go, and what about mycommandVO Commented Mar 11, 2017 at 21:09
  • 41
    I would suggest underscores for long names. Have seen this in some good projects.
    – Avi
    Commented Jun 9, 2017 at 3:35
27

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.

5
  • 8
    In the link you provided, I found use cases with the underscore: golang.org/src/pkg/compress/bzip2/move_to_front.go, golang.org/src/pkg/compress/flate/huffman_bit_writer.go, golang.org/src/pkg/compress/flate/reverse_bits.go.
    – david
    Commented Aug 6, 2014 at 15:06
  • 2
    I guess they won't use _front, _writer or _bits as significant suffixes in the future then! Commented Aug 6, 2014 at 15:47
  • I love Go, but the go tool is very restrictive about package structure (it's one of my favorite things about the language). It favors some very specific conventions (one package per folder [with at least one exception], the folder's package shares the same name as the folder [with at least one exception], full package import path matches the relative path from $GOPATH, some files are treated differently depending on their name format, etc)
    – weberc2
    Commented Aug 6, 2014 at 19:30
  • 1
    @weberc2 The restrictions are analogous to Latex. First, I wanted to control my layout and other irrelevant details, until I realised that all that needs to be written is good content. Similarly, Go enables us to write good code and handles other details for us.
    – david
    Commented Aug 7, 2014 at 10:24
  • 1
    @david I agree. In retrospect I was unclear: I was responding to the answerer's statement Go is quite liberal in terms of how you organise your code within a package. Go is not liberal, it's quite restrictive. But that's a good thing.
    – weberc2
    Commented Aug 7, 2014 at 13:58
11

Usually underscore in filenames are used to assign platform/arch-only code, for example:

➜ cd $GOROOT/src/pkg/math/
➜ ls sqrt*s
sqrt_386.s  sqrt_amd64p32.s  sqrt_amd64.s  sqrt_arm.s

sqrt_386.s will only be read by the compiler on 32bit processors, sqrt_amd64.s on amd64, etc..

It can be any of the valid values of GOOS and/or GOARCH (ref.

file_windows_amd64.go will be only compiled on win64.

3

Long all small caps for filenames is a terrible idea!

Filenames do not have semantic meaning at go - they are not imported or referenced. Code files in go are compiled to the package they are assigned to as one unit.

Since the article in effective go don't include limitations on muti word filenames except underscore (for build, ignore, test and compile purposes) mixedCaps are the best option for multi word filenames convention. And it's recommend by the same article "effective go"

Tl;dr:

- smallallcupspackagename
|- veryLongFileName.go
|- veryLongFileName_test.go
|- veryLongFileName_amd64_windows.go
1

apart from all the other suggestions, the naming conventions should include (.). what it means is that if you have a module as "User", you are using routes, and models are defined inside the user module. Their name should be like user.models.go, user.route.go, user.service.go. This is just clearer and easier to read.

1
  • 2
    This is not a convention. No delimiter is preffered.
    – States
    Commented May 27, 2022 at 3:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.