Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Standard library

Goblin's standard library provides modules for common program boundaries: files, environment variables, external commands, paths, time, data formats, networking, random values, mathematics, and MIME metadata. Import a module at module scope, then access its members with dot notation.

import "json"
import "path"

var cwd = path.cwd()
var text = json.marshal({"cwd": cwd})
print(text)

The standard library is separate from Goblin's built-in functions and types. For example, print(), eprint(), range(), Int(), List(), Dict(), and Chan() are available without an import. Import a module only when its capabilities are needed.

Two tiers: core and x/

The standard library has two tiers. Core modules have curated, Goblin-shaped APIs and simple names such as "json" and "fs". Modules under the x/ prefix are direct adaptations of Go packages and keep Go's package hierarchy in their import path, so compress/gzip becomes x/compress/gzip. In both tiers the imported name is the last path component:

import "x/compress/gzip"

var packed = gzip.compress("hello")

Core modules

ModuleMain purposeStart with
jsonEncode and decode JSONmarshal(), unmarshal()
fsRead, write, inspect, and remove filesread(), write(), exists()
osRead environment and process informationargv(), getenv(), getwd(), hostname()
execConfigure and execute external commandsCommand()
pathFind the current or home directorycwd(), home()
timeWork with time and durationsnow(), sleep(), parse()
randGenerate reproducible random values and permutationsRand(), int(), shuffle()
mathNumeric constants and functionspi, sqrt(), pow(), abs()
httpMake HTTP requestsget(), post(), put()
uuidConstruct, generate, and validate UUID valuesUUID(), new(), is_valid()
regexpSearch, capture, replace, and split text with RE2 expressionscompile(), escape()
urlParse, resolve, join, and escape URLsparse(), query_escape()
csvRead and write comma-separated recordsread_all(), write_all()

x/ modules

ModuleMain purposeStart with
x/encoding/base64Encode and decode Base64 textencode(), decode()
x/encoding/base32, ascii85, html, quotedprintableEncode text and escape HTMLencode(), escape()
x/encoding/hexEncode, decode, and dump hexadecimal dataencode(), decode()
x/encoding/pemEncode and decode PEM blocksBlock(), decode()
x/mimeLook up MIME types and extensionstype_by_extension()
x/crypto/sha256 and sha512Compute fixed-size SHA-2 digestssum(), hex()
x/crypto/md5, sha1, x/hash/crc32, adler32Compute compatibility digests and checksumshex(), checksum()
x/crypto/hmac, x/hash/crc64, fnvCompute keyed and non-cryptographic hashessum(), hex()
x/compress/gzip, zlib, flate, bzip2Compress and decompress complete byte valuescompress(), decompress()
x/compress/lzwCompress and decompress LZW datacompress(), decompress()
x/archive/tar and zipRead and write complete in-memory archivesread_all(), write_all()
x/net/mailConstruct and parse email addressesparse_address()
x/net/netipParse and calculate with IP addresses and prefixesAddr(), Prefix()
x/unicode and x/unicode/utf8Validate UTF-8 and classify Unicode charactersvalid(), is_letter()

Imports and errors

Standard-library module names never start with "./" or "../"; core names are plain ("json", "fs") and x/ names carry their Go-style path ("x/compress/gzip"). Local source modules use a relative import such as "./modules/greeter"; those are documented in Modules and imports because they use the same import syntax.

Most standard-library operations that touch the outside world can fail. JSON parsing may raise ParseError, a missing file may raise an I/O-related error, and HTTP requests may fail. Use try/catch around work that your program can recover from; see Errors.

Choosing a module

Use json whenever a program boundary expects JSON rather than trying to build JSON text manually. Prefer fs for simple whole-file reads and writes, and use its open() function when a file object is needed. Use path.cwd() or path.home() instead of assuming a current directory. Use time.sleep() only for intentional delays, and use Chan plus spawn() for communication between concurrent Goblin functions.

Each module has its own chapter in this section, with a focused API reference and example.

Reading API signatures

Examples and tables use name(required, optional=value) to show argument order and defaults. Square brackets mean an argument may be omitted, as in Chan([size]). They do not promise that named arguments are accepted: a function's chapter calls out positional-only APIs where that matters.

Unless a chapter says otherwise, a function that touches files, the operating system, or the network can raise an error value. Wrap the smallest useful boundary in try/catch, then add context or recover deliberately.