snippetgoCritical
How to reliably detect os/platform in Go
Viewed 0 times
reliablyhowplatformdetect
Problem
Here's what I'm currently using, which I think gets the job done, but there's got to be a better way:
As you can see, in my case all I need to know is how to detect windows but I'd like to know the way to detect any platform/os.
Play:
http://play.golang.org/p/r4lYWDJDxL
func isWindows() bool {
return os.PathSeparator == '\\' && os.PathListSeparator == ';'
}As you can see, in my case all I need to know is how to detect windows but I'd like to know the way to detect any platform/os.
Play:
http://play.golang.org/p/r4lYWDJDxL
Solution
Detection at compile time
If you're doing this to have different implementations depending on the OS, it is more useful to
have separate files with the implementation of that feature and add build tags to each
of the files. This is used in many places in the standard library, for example in the
These so-called "Build constraints" or "Build tags" are explained here.
Say you have the constant
would make two files, one for Windows and one for the (UNIX) rest:
The code of these files would then be:
You can now access
Detection at runtime
If you want to determine the operating system at runtime, use the
variable:
While this is compiled into the runtime and therefore ignores the environment,
you can nevertheless be relatively certain that the value is correct.
The reason for this is that every platform that is worth distinguishing needs
rebuilding due to different executable formats and thus has a new
If you're doing this to have different implementations depending on the OS, it is more useful to
have separate files with the implementation of that feature and add build tags to each
of the files. This is used in many places in the standard library, for example in the
os package.These so-called "Build constraints" or "Build tags" are explained here.
Say you have the constant
PATH_SEPARATOR and you want that platform-dependent, youwould make two files, one for Windows and one for the (UNIX) rest:
/project/path_windows.go
/project/path_unix.goThe code of these files would then be:
path_windows.go// +build windows
package project
const PATH_SEPARATOR = '\\'path_unix.go// +build !windows
package project
const PATH_SEPARATOR = '/'You can now access
PATH_SEPARATOR in your code and have it platform dependant.Detection at runtime
If you want to determine the operating system at runtime, use the
runtime.GOOSvariable:
if runtime.GOOS == "windows" {
fmt.Println("Hello from Windows")
}While this is compiled into the runtime and therefore ignores the environment,
you can nevertheless be relatively certain that the value is correct.
The reason for this is that every platform that is worth distinguishing needs
rebuilding due to different executable formats and thus has a new
GOOS value.Code Snippets
/project/path_windows.go
/project/path_unix.go// +build windows
package project
const PATH_SEPARATOR = '\\'// +build !windows
package project
const PATH_SEPARATOR = '/'if runtime.GOOS == "windows" {
fmt.Println("Hello from Windows")
}Context
Stack Overflow Q#19847594, score: 218
Revisions (0)
No revisions yet.