Skip to content

@os

Import this module with import @os.

Function Signature Description
get_env (name string) -> string Get environment variable. Returns "" for both an unset variable and one explicitly set to ""
lookup_env (name string) -> (string, bool) Get environment variable and whether it is actually set, distinguishing an unset variable from one set to ""
set_env (name string, value string) Set environment variable
unset_env (name string) Remove environment variable
environ () -> [string] Every environment variable of the current process, as "KEY=VALUE" strings
Function Signature Description
args () -> [string] Get command-line arguments
current_dir () -> string Get current working directory
home_dir () -> string Get the current user’s home directory ($HOME on Unix, %USERPROFILE% on Windows)
hostname () -> string Get machine hostname
pid () -> i64 Get process ID
current_os () -> Platform Get the current OS as a Platform enum value
arch () -> string Get CPU architecture
cpu_count () -> i64 Number of logical CPUs available to the process; falls back to 1
is_tty () -> bool True if standard output is a terminal (not a file or pipe)
Function Signature Description
exec (cmd string, args [string]) -> (i64, string, string, bool) Run a subprocess. Returns (exit_code, stdout, stderr, ok). ok is false if the process could not be launched. stdout and stderr are captured. POSIX only.
import @os
do main() {
mut code, stdout, stderr, ok = os.exec("ls", {"-l", "/tmp"})
if !ok {
println("failed to launch")
}
if ok && code != 0 {
println("exited with error: ${stderr}")
}
}

os provides the Platform enum for the mutually-exclusive host-OS values. Reachable as os.MAC_OS or Platform.MAC_OS (both denote the same value); returned by current_os(). Underlying values: MAC_OS 0, LINUX 1, WINDOWS 2, OTHER 3. Platform is reserved as a type name only while @os is imported.

import @os
do main() {
when os.current_os() {
is .LINUX { println("linux") }
is .MAC_OS { println("macos") }
default { println("other") }
}
}