A File Naming Convention (FNC) is a framework for naming your files in a way that describes what they contain and how they relate to other files. Developing an FNC is done through identifying the key elements of the project, the important differences and commonalities between your files.
Why use naming conventions? Naming records consistently, logically and in a predictable way will distinguish similar records from one another at a glance, and by doing so will facilitate the storage and retrieval of records, which will enable users to browse file names more effectively and efficiently.
There's a few guidelines to follow.
_test.go
are only compiled and run by the go test
tool.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 fileSee the go
docs for more details: https://pkg.go.dev/cmd/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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With