← All languages
Go function of the day
Random

runtime.GOOS

The running program's operating system target as a string constant.

Description

runtime.GOOS is a string constant identifying the operating system target the binary was built for. Common values include "linux", "darwin" (macOS), "windows", "freebsd", "netbsd", "openbsd", "plan9", "solaris", "android", and "ios".

Because it is a constant set at build time, branches keyed on GOOS can be eliminated by the compiler when the target is known. For more granular build-time selection, use build constraints (e.g., //go:build linux) or filename suffixes (foo_linux.go), which avoid runtime branching entirely.

The companion constant runtime.GOARCH gives the target architecture (amd64, arm64, etc.). Together they are used for OS-specific paths, feature toggles, and platform reporting in logs and version strings.

Example

import "runtime"

fmt.Println("Running on", runtime.GOOS)

switch runtime.GOOS {
case "windows":
    cmd = "cmd.exe"
default:
    cmd = "sh"
}

Reference