Базовая стандартная библиотека
Shipped stdlib surface — вывод, файловый I/O, коллекции, JSON, HTTP и принцип explicit-by-design.
Текущий stdlib surface намеренно небольшой.
Shipped stdlib теперь имеет реальный import-delivery path. Зарезервированные module names:
bytes,io,time,net,http
Shipped bytes, stream, deadline и TCP foundation
bytes—Bytes,bytes_from_string,bytes_to_string,bytes_len,bytes_slice,bytes_concatio—ReadStream,WriteStream,open_read_stream,open_write_stream,ReadStream.read_all_string,WriteStream.write_all_string,ReadStream.with_timeout,WriteStream.with_timeouttime—NetDeadline,deadline_after,deadline_at_unix_millisnet—DuplexStream,TcpListener,SocketAddr,connect_tcp,connect_tcp_loopback,connect_tcp_with_control,connect_tcp_with_timeout,connect_tcp_with_timeout_budget,connect_tcp_loopback_with_control,connect_tcp_loopback_with_timeout,connect_tcp_loopback_with_timeout_budget,listen_tcp,listen_tcp_with_timeout,listen_tcp_loopback,listen_tcp_loopback_with_timeout,DuplexStream.read_exact_string,DuplexStream.write_all_string,DuplexStream.with_timeout,TcpListener.with_timeout,TcpListener.accept_with_timeout,TcpListener.accept_with_timeout_budget,SocketAddr.connect_tcp,SocketAddr.connect_tcp_with_control,SocketAddr.connect_tcp_with_timeout,SocketAddr.connect_tcp_with_timeout_budget
net теперь также ship-ит loopback helper-ы и typed SocketAddr connect wrapper-ы, чтобы локальный service code не собирал "127.0.0.1:" + to_string(port) и address.text() вручную.
io и net теперь также ship-ят Result-returning timeout wrapper-ы, timeout-armed constructor-ы и single-budget helper-ы вроде open_write_stream_with_timeout(...), listener.with_timeout(...), listen_tcp_loopback_with_timeout(...), address.connect_tcp_with_timeout_budget(...) и listener.accept_with_timeout_budget(...), чтобы не повторять deadline_after(...) и немедленный stream.with_timeout(...) в каждом локальном service path.
import bytes
import io
import time
fn main() -> Result[int, RuntimeError]:
deadline = deadline_after(1000)?
writer = open_write_stream_with_timeout("roundtrip.txt", 1000)?
writer.write_all_string("hello")?
writer.flush()?
writer.close()?
reader = open_read_stream_with_timeout("roundtrip.txt", 1000)?
text = reader.read_all_string()?
reader.close()?
return Result.Ok(len(text) + deadline.unix_millis() - deadline.unix_millis())import net
fn main() -> Result[int, RuntimeError]:
listener = listen_tcp_loopback_with_timeout(0, 1000)?
address = listener.local_addr()?
client = address.connect_tcp_with_timeout_budget(1000)?
server = listener.accept_with_timeout_budget(1000)?
client.write_all_string("ping")?
text = server.read_exact_string(4)?
return Result.Ok(address.port() + len(text) - len(text))Blocking I/O возвращает Result[..., RuntimeError], а не прячет ошибку в неявном runtime state.
String helper-ы, timeout wrapper-ы и single-budget connect helper-ы не скрывают ошибки декодирования и настройки: они тоже возвращают Result[...].
Output и correctness checks
print("ready")
assert(true, "must stay true")Process, filesystem и paths
fn main() -> Result[int, RuntimeError]:
root = cwd()?
path = path_join(root, "target/demo.txt")
write_file(path, "gof")?
return Result.Ok(len(read_file(path)?))argv(),read_stdin(),read_stdin_lines()unix_seconds(),unix_millis()env(name),cwd()run_process(program, args)— без shell-интерполяцииread_file(path),write_file(path, contents)exists(path),read_dir(path),mkdir(path),remove_file(path)path_join,path_dir,path_base,path_ext
Helpers для коллекций
values = [1, 2]
values = append(values, 3)
has_three = contains(values, 3)
window = slice(values, 0, 2)
ordered = sort(reverse(values))store: dict = {"critical": 5, "ok": 7}
names = keys(store)
counts = values(store)
present = contains(store, "ok")line = trim(" gof,lang ")
parts = split(line, ",")
merged = join(parts, "-")
has_prefix = starts_with(merged, "gof")
has_suffix = ends_with(merged, "lang")Sequence helpers
first(list),last(list)→Result[element, RuntimeError]slice(list, start, end)→Result[list[element], RuntimeError]reverse(list),sort(list)→ новые спискиmin(list),max(list)→Result[element, RuntimeError]
JSON, CSV, TOML, YAML, base64, HTTP
json_parse,json_get,json_index,json_len,json_string,json_intcsv_parse,csv_stringifytoml_parse,yaml_parsebase64_encode,base64_decodetemplate_render(template, values)http_request(method, url[, body[, headers[, timeout_ms]]])import http— JSON request helper-ы вродеrequest_json_headers(),request_headers_set(...),request_headers_merge(...),request_json_headers_with(...),request_bearer_headers(token),request_bearer_headers_with(token, extra),request_json_bearer_headers(token),request_json_bearer_headers_with(token, extra),get_json(url),get_json_with_timeout(url, timeout_ms),get_json_with_headers(url, headers, timeout_ms),post_json(url, body),post_json_with_timeout(url, body, timeout_ms),post_json_with_headers(url, body, headers, timeout_ms),request_report(...),request_report_with_headers(...),get_report(url, timeout_ms),get_report_with_headers(url, headers, timeout_ms),post_report(url, body, timeout_ms),post_report_with_headers(url, body, headers, timeout_ms),request_json_report(...),request_json_report_with_headers(...),request_json_with_headers(...)import http— typed response helpers вродеresponse_status(...),response_status_class(...),response_is_success(...),response_require_success(...),response_json(...),response_json_success(...),response_headers(...),response_content_type(...)http_get(url),http_post(url, body[, content_type])
Когда нужен request shape шире, чем покрывают shipped parsed helper-ы, или когда надо сначала вручную осмотреть non-2xx ответ, оставайся на raw http_request(...), get_report..., post_report... / request_json_report... path.
sleep(milliseconds)— явный retry/backoff
Conversion helpers
fn main() -> Result[int, RuntimeError]:
parsed = parse_int(trim(" 41 "))?
rendered = "gof-" + to_string(parsed + 1)
return Result.Ok(parsed + len(rendered))