Use Simple Branching in Trivial Cases

Use simple branching in trivial cases.

A simplest form of cross-platform code is setting a variable within an if or switch statement depending on values of runtime.GOOS, runtime.GOARCH or runtime.Version().

Such code does not require any build constraints (reminder, build constraints can be expressed as a suffix in the name of a file, or as a build tag). The result depends on the values of the corresponding constants or the returned value of the Version() function, which in turn depend on the system where the program is running and/or version of Go used during the build process.

However, it's tempting to abuse this method. Beware that it is only helpful in very basic cases like choosing the appropriate extension of a file name or something similar. Never implement different behaviour based on branching depending on the aforementioned values. This leads to fragile architecture and complicates testing, debugging and maintenance overall.

This method helps to choose what path the execution will follow at runtime, but it does not imply conditional compilation. If implementations vary between platforms, and are not available on them at the same time, then builds will be broken.

Essentially, one of the most reasonable uses of this option is to determine and/or report what actual platform the code is running on.

Don't confuse runtime conditions with compile-time conditions.