Skip to content

lib/os

import "github.com/eugenioenko/goalchemy/lib/os"

Package os reads and writes whole host files and opens files as handles.

Function c csharp go java python rust swift typescript
Close yes yes yes yes yes yes yes yes
CreateTemp yes yes yes yes yes yes yes yes
Open yes yes yes yes yes yes yes yes
Read yes yes yes yes yes yes yes yes
ReadAt yes yes yes yes yes yes yes yes
ReadFile yes yes yes yes yes yes yes yes
Remove yes yes yes yes yes yes yes yes
Rename yes yes yes yes yes yes yes yes
Seek yes yes yes yes yes yes yes yes
Stat yes yes yes yes yes yes yes yes
Sync yes yes yes yes yes yes yes yes
Write yes yes yes yes yes yes yes yes
WriteFile yes yes yes yes yes yes yes yes
func Close(file int64) int

Closes a file handle and releases it.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Errors: An unknown or already closed handle reports Invalid; a host failure while closing IO.
  • Contract: lib.os.close 1.0.0
func CreateTemp(dir string, prefix string, suffix string) (int64, string, int)

Creates a new uniquely named file in a directory, opened for reading and writing.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: The random part is one to sixteen characters from [0-9a-z]. A prefix or suffix containing a path separator, or a NUL byte in any argument, reports Invalid. The host retries a name that already exists, as Go does.
  • Errors: A missing dir NotExist; access denial Permission; a dir that is not a directory NotDir; hosts without a file system Unsupported; other host failures IO.
  • Contract: lib.os.create_temp 1.0.0
func Open(name string, flag int, perm uint32) (int64, int)

Opens a host file with access and modifier flags and returns a file handle.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: flag is a combination of RDONLY 0, WRONLY 1, RDWR 2, APPEND 4, CREATE 8, EXCL 16 and TRUNC 32; any other bit, or WRONLY with RDWR, reports Invalid. perm keeps its low nine bits and applies only to a file CREATE creates, minus the process umask on POSIX hosts.
  • Errors: Missing paths report NotExist (or, without CREATE, for a missing final component); CREATE with EXCL on an existing path reports Exist; access denial Permission; a non-directory path component NotDir; hosts without a file system Unsupported; other host failures IO.
  • Contract: lib.os.open 1.0.0
func Read(file int64, max int) ([]byte, bool, int)

Reads up to max bytes from a file handle’s offset and reports the end of the file.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: max is 1..1048576 (MaxChunk); other values report Invalid. A read may return fewer bytes than max before the end.
  • Errors: Reading a directory reports IsDir; a handle opened write-only reports Permission or IO as the host reports it; unknown handles Invalid; other host failures IO.
  • Contract: lib.os.read 1.0.0
func ReadAt(file int64, offset int64, max int) ([]byte, bool, int)

Reads up to max bytes at an offset without moving the handle’s offset.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: max is 1..1048576 (MaxChunk) and offset is non-negative; other values report Invalid.
  • Errors: As lib.os.read.
  • Contract: lib.os.read_at 1.0.0
func ReadFile(name string) ([]byte, int)

Reads a whole host file and reports a portable status code.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: Files larger than MaxFileBytes (1 GiB) report TooLarge without returning data.
  • Errors: Missing paths report NotExist; access denial Permission; reading a directory IsDir; a non-directory path component NotDir; hosts without a file system Unsupported; other host failures IO.
  • Contract: lib.os.read_file 1.0.0
func Remove(name string) int

Removes a named file or empty directory.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Errors: Missing paths NotExist; access denial Permission; a non-empty directory reports what the host reports, Permission or IO; hosts without a file system Unsupported; other host failures IO.
  • Contract: lib.os.remove 1.0.0
func Rename(oldpath string, newpath string) int

Renames a file, replacing an existing file at the new path.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Errors: A missing oldpath or a missing parent of newpath NotExist; access denial Permission; a non-directory path component NotDir; hosts without a file system Unsupported; other host failures IO.
  • Contract: lib.os.rename 1.0.0
func Seek(file int64, offset int64, whence int) (int64, int)

Moves a file handle’s offset and returns the new offset.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: whence is 0 (start), 1 (current) or 2 (end); other values, or a resulting offset below zero, report Invalid. Offsets past the end are allowed, as in Go.
  • Errors: Unknown handles Invalid; other host failures IO.
  • Contract: lib.os.seek 1.0.0
func Stat(name string) (int64, int, bool, int)

Reports a named file’s size, permission bits and whether it is a directory.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: mode is the low nine permission bits; hosts without POSIX permissions report 0o666 for files and 0o777 for directories, or 0.
  • Errors: Missing paths NotExist; access denial Permission; a non-directory path component NotDir; hosts without a file system Unsupported; other host failures IO.
  • Contract: lib.os.stat 1.0.0
func Sync(file int64) int

Flushes a file handle’s written data to the storage device.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Errors: Unknown handles Invalid; a read-only handle or a host that cannot sync reports what the host reports, Invalid or IO.
  • Contract: lib.os.sync 1.0.0
func Write(file int64, data []byte) (int, int)

Writes data at a file handle’s offset, or at the end of a file opened with APPEND.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: data is at most 1048576 bytes (MaxChunk); more reports Invalid.
  • Errors: On failure written is the count accepted before it. A handle opened read-only reports Permission or IO as the host reports it; unknown handles Invalid; a full device or quota IO.
  • Contract: lib.os.write 1.0.0
func WriteFile(name string, data []byte, perm uint32) int

Creates or truncates a host file and writes data, reporting a portable status code.

  • Gate: sequential (never suspends)
  • Determinism: nondeterministic
  • Bounds: Data larger than MaxFileBytes (1 GiB) reports TooLarge without touching the file.
  • Errors: A missing parent directory reports NotExist; access denial Permission; a directory name IsDir; a non-directory path component NotDir; hosts without a file system Unsupported; other host failures IO. A failure after truncation can leave a partial file, as in Go.
  • Contract: lib.os.write_file 1.0.0