Use File Suffix by Default
Use the file suffix to manage cross-platform code by default, i.e. when the conditions for compilation are simple.
What this suggestion means is that in simple cases where the platforms you need to support are discrete and don't share commonalities, the simplest possible way is to use suffixes with files containing code for a particular platform, be it different operating systems, architectures, or a combination of the two criteria.
For instance, suppose you have a CLI application that must support Linux and Windows, and the architecture does not matter (as in amd64 or 386), and something requires a different implementation for each of the platforms. In this case, we have a simplest condition possible – two different operating systems. The best way to express it is to use suffixes:
└── something
├── something.go
├── something_linux.go
└── something_windows.go
Nothing else is required. Not even a single tag. Then, as discussed earlier, there are two ways to build it:
- on the same machine, using cross compilation:
# For Linux.
GOOS=linux go build -o bin/myapp-linux ./cmd/myapp
# For Windows.
GOOS=windows go build -o bin/myapp-windows.exe ./cmd/myapp
- on machines with the corresponding platforms the environment variable is not needed:
# On Linux.
go build -o bin/myapp-linux ./cmd/myapp
# On Windows.
go build -o bin/myapp-windows.exe ./cmd/myapp