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)
find_executable (name string) -> (string, bool) Full path of the first executable regular file called name on PATH, and whether one was found. A name containing a path separator is checked directly instead of searched. On Windows the PATHEXT extensions are tried and PATH is split on ;
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)
executable_path () -> (string, Error) Absolute path of the running binary; Error is IoFailure if it cannot be determined, Unsupported on a platform without a way to ask
cpu_usage (interval_ms i64) -> (f64, Error) Busy percentage (0.0–100.0) of all logical CPUs over a window of interval_ms; blocks for the window; panics if interval_ms is less than 1
memory_usage () -> (i64, i64, Error) (used, total) bytes of physical memory; used excludes memory available to new allocations
disk_usage (path string) -> (i64, i64, Error) (used, total) bytes of the filesystem containing path; a missing path returns (0, 0) and a NotFound Error
load_average () -> ([f64], Error) 1, 5 and 15 minute load averages

The four live metrics are fallible. macOS and Linux are supported; on any other platform (Windows) each returns an Error with code Unsupported.

Function Signature Description
exec (cmd string, args [string]) -> (ExecResult, Error) Run a subprocess and capture its output. err is non-nil only when the process could not be launched; a non-zero exit code is not an error.

ExecResult has the fields exit_code (i64), stdout (string), stderr (string) and signal (i64). signal is the number of the signal that terminated the process, or 0 when it exited normally; it is always 0 on Windows. It is reserved as a type name only while @os is imported.

import @os
do list_tmp() {
mut result, err = os.exec("ls", {"-l", "/tmp"})
if err != nil {
println("failed to launch: ${err.message}")
return
}
if result.exit_code != 0 {
println("exited with error: ${result.stderr}")
}
}
do main() {
list_tmp()
}

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") }
}
}