Skip to content

Os.platform ​

ant
func Os.platform(): string

Os.platform returns the host's operating system by Go's name for it, GOOS: "darwin", "linux", "windows", and so on.

Details ​

  • The name is Go's, always lower case: "darwin" for macOS, never "macos" or "Darwin" as uname prints it.

Basic examples ​

Print the operating system of the host. On a Mac it prints darwin:

ant
println(Os.platform())

Compare it with a name to choose a value by platform:

ant
let opener = if Os.platform() == "darwin" { "open" } else { "xdg-open" }
println(opener)

Scope ​

Look the name up in a map to show it to a person, keeping any other name as it is:

ant
let names = Map{ "darwin": "macOS", "linux": "Linux", "windows": "Windows" }
let os = Os.platform()
println(names[os] ?? os)

Join it with Os.arch to name the whole target, such as darwin-arm64 or linux-amd64:

ant
println("${Os.platform()}-${Os.arch()}")

Pass it as a function value:

ant
let current: func(): string = Os.platform
assert(current() == Os.platform())

Applications ​

Name the executable a build produces, which carries .exe on Windows:

ant
let exe = if Os.platform() == "windows" { "tool.exe" } else { "tool" }
println(exe)

Pick the command that opens a file in its default application, with an if expression that chooses among the platforms:

ant
let os = Os.platform()
let opener = if os == "darwin" { "open" } else if os == "windows" { "explorer" } else { "xdg-open" }
println(Cmd.string(Cmd.new(opener, ["report.html"])))

Split $PATH by the separator the platform uses, ; on Windows and : elsewhere:

ant
let sep = if Os.platform() == "windows" { ";" } else { ":" }
let dirs = (Os.env("PATH") ?? "").split(sep)
println("${dirs.length()} directories on the PATH")

Properties and relations ​

The name is never empty and is lower case:

ant
let os = Os.platform()
assert(!os.isEmpty())
assert(os == os.toLower())

Os.uid and Os.gid are null on Windows, which has no user or group ids, and on other hosts without them, such as WebAssembly:

ant
if Os.platform() == "windows" {
  assert(Os.uid() == null)
}
assert((Os.uid() == null) == (Os.gid() == null))

Possible issues ​

A comparison with the name a person would write, or the one uname prints, is never true:

ant
assert(Os.platform() != "macos")
assert(Os.platform() != "Darwin")

See also ​

Os.arch · Os.uid · Os.gid · Os.env · Cmd.new · Os