Os.platform
func Os.platform(): stringOs.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"asunameprints it.
Basic examples
Print the operating system of the host. On a Mac it prints darwin:
println(Os.platform())Compare it with a name to choose a value by platform:
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:
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:
println("${Os.platform()}-${Os.arch()}")Pass it as a function value:
let current: func(): string = Os.platform
assert(current() == Os.platform())Applications
Name the executable a build produces, which carries .exe on Windows:
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:
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:
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:
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:
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:
assert(Os.platform() != "macos")
assert(Os.platform() != "Darwin")