Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reliably detect os/platform in Go

Here's what I'm currently using, which I think gets the job done, but there's got to be a better way:

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

like image 726
mdwhatcott Avatar asked Nov 07 '13 22:11

mdwhatcott


4 Answers

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 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, you would make two files, one for Windows and one for the (UNIX) rest:

/project/path_windows.go
/project/path_unix.go

The 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.GOOS variable:

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.

like image 96
nemo Avatar answered Nov 06 '22 04:11

nemo


Have you looked at the runtime package? It has a GOOS const: http://golang.org/pkg/runtime/#pkg-constants

like image 21
Tyler Egeto Avatar answered Nov 06 '22 02:11

Tyler Egeto


It's 2022 and the correct answer for go 1.18+ is:

At runtime you want:

if runtime.GOOS == "windows" {
  // windows specific code here...
}

If you need to determine the filesystem path separator character

Use: os.PathSeparator

Examples:

  • c:\program files
  • /usr/local/bin

If you need the Path List separator as used by the PATH environment variable

Use: os.PathListSeparator

Examples:

  • /usr/local/bin:/usr/local:
  • "C:\windows";"c:\windows\system32";
like image 4
hookenz Avatar answered Nov 06 '22 03:11

hookenz


Since this is an older question and answer I have found another solution.

You could simply use the constants defined in the os package. This const returns a rune so you would need to use string conversion also.

string(os.PathSeparator)
string(os.PathListSeparator)

Example: https://play.golang.org/p/g6jnF7W5_pJ

like image 2
ENG618 Avatar answered Nov 06 '22 03:11

ENG618