Language guide · 12 of 12

Standard Library

Import a package by name: package main imports("math", "json"). Functions marked yields wait on the outside world and let other tasks run while they wait (see Shared State under Concurrency); no other standard function yields. Arguments of the wrong type raise an error that names the function and the argument.

io

Reads and writes use the engine's Options.Stdin and Options.Stdout (defaulting to os.Stdin and os.Stdout).

function does
print(...values), println(...values) write the values separated by spaces
prompt(message) print a message and read a line (nil at EOF); yields
readln() read a line (nil at EOF); yields

math

function does
pi, e, inf, nan, maxSafeInt constants
abs, sign, floor, ceil, round, trunc rounding and sign
sqrt, cbrt, pow(x, y), exp, log, log2, log10, hypot(x, y) powers and logarithms
sin, cos, tan, asin, acos, atan, atan2(y, x), sinh, cosh, tanh trigonometry, in radians
min(...), max(...) smallest or largest of the arguments, or of one array
clamp(x, lo, hi) x limited to the range
isNaN(x), isInf(x), isFinite(x) checks
random(), randomInt(lo, hi) a number in [0, 1), an integer in [lo, hi]

fs

function does
readFile(path), readText(path) a file as a buffer or as a string; yields
writeFile(path, content), appendFile(path, content) write or append a string or buffer; yields
exists(path), isDir(path) tests; yields
list(dir) sorted names in a directory; yields
mkdir(dir) create a directory and its parents; yields
remove(path), rename(from, to) remove a file or directory tree, move it; yields
join(...parts), base(path), dir(path), ext(path) path helpers; these do not touch the disk and do not yield

time

Times are milliseconds since the Unix epoch, so they compare and subtract like any number.

function does
now(), since(t) the current time, milliseconds elapsed since t
clock() a monotonic clock in milliseconds, for measuring
wait(ms) (also sleep, timer) pause the current task; yields
format(t), format(t, layout) UTC RFC 3339 text, or a Go layout such as "2006-01-02"
parse(text), parse(text, layout) parse an RFC 3339 time or a custom Go layout
second, minute, hour, day durations in milliseconds

json

function does
json.encode(value), json.decode(text) convert between values and JSON text (rejects NaN/Infinity, functions, tasks, errors, and cycles)

Edit this page on GitHub