How to reliably detect os/platform in Go

Operating SystemGo

Operating System Problem Overview


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

Operating System Solutions


Solution 1 - Operating System

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.

Solution 2 - Operating System

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

Solution 3 - Operating System

I just stumbled on this looking for something else and noticed the age of this post so I'll add a more updated addition. If you're just trying to handle the correct filepath I would use filepath.Join(). Its takes all of the guesswork out of os issues. If there is more you need, other than just filepath, using the runtime constants (runtime.GOOS & runtime.GOARCH) are the way to go: playground example

Solution 4 - Operating System

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

Solution 5 - Operating System

I tested in Go 1.17.1 which really worked for me.

package main

import (
        "fmt"
        "runtime"
)
func main(){
        fmt.Println(runtime.GOOS)
}

Output:
darwin

Solution 6 - Operating System

With regards to detecting the platform, you can use Distribution Detector project to detect the Linux distribution being run.

Solution 7 - Operating System

The first answer from @nemo is the most apropiate, i just wanted to point out that if you are currently a user of gopls language server the build tags may not work as intended.

There's no solution or workaround up to now, the most you can do is change your editor's lsp configs (vscode, neovim, emacs, etc) to select a build tag in order to being able to edit the files with that tag without errors. Editing files with another tag will not work, and trying to select multiple tags fails as well. This is the current progress of the issue github@go/x/tools/gopls

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmdwhatcottView Question on Stackoverflow
Solution 1 - Operating SystemnemoView Answer on Stackoverflow
Solution 2 - Operating SystemTyler EgetoView Answer on Stackoverflow
Solution 3 - Operating SystemBill.CafferyView Answer on Stackoverflow
Solution 4 - Operating SystemENG618View Answer on Stackoverflow
Solution 5 - Operating SystemDr.Mr.DrView Answer on Stackoverflow
Solution 6 - Operating SystemElijahView Answer on Stackoverflow
Solution 7 - Operating SystemFrancisco IthurriagueView Answer on Stackoverflow