XU XUv0.16.0

#os

import: #os

counts: 84 functions, 16 methods, 5 types, 9 constants, 0 method sets

Process, files, environment. Hosted File: open/create/read/write/close. getwd returns empty on failure.

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

36_test_sqlite_pool/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@main

sqlite #github.com/xulang-org/packages/sqlite
#database/sql
#os
#errors

fn remove_if_exists(p string) {
    os.remove(p) >>> (e) {
        <<
    }
}

fn verify_sqlite_pool_deep() {
    println("--- SQLite Connection Pool Deep Verification ---")
    db_file := "/tmp/xu_sqlite_pool.db"
    remove_if_exists(db_file + "")
    $db := sqlite.open_pool(db_file + "", 5, 2)

    // 1. Ping
    db.ping() >>> (err) {
        println("   [FAIL] SQLite pool ping failed: " + err.msg())
        <<
    }
    println("   [PASS] SQLite Pool ping succeeded.")

    // 2. Schema
    db.exec("CREATE TABLE pool_items (id INTEGER PRIMARY KEY, title TEXT, value INT);") >>> (err) {
        println("   [FAIL] Create table failed: " + err.msg())
        <<
    }
    println("   [PASS] Schema created.")

    // 3. Sequential & Concurrent Inserts through Pool
    $i := 1
    while i <= 5 {
        db.exec("INSERT INTO pool_items (id, title, value) VALUES (?, ?, ?);", i, "item_" + str(i), i * 10) >>> (err) {
            println("   [FAIL] Insert failed at item " + str(i) + ": " + err.msg())
            <<
        }
        i = i + 1
    }
    println("   [PASS] 5 items inserted through SQLite connection pool.")

    // 4. Query through Pool
    $rows := db.query("SELECT id, title, value FROM pool_items ORDER BY id ASC;") >>> (err) {
        println("   [FAIL] Query failed: " + err.msg())
        <<
    }
    $count := 0
    while rows.next() {
        vals := rows.take() >>> (err) {
            <<
        }
        count = count + 1
    }
    rows.close()
    println("   [PASS] SQLite Pool query verified: retrieved " + str(count) + " rows.")

    // 5. Close Pool
    db.close() >>> (err) {
        println("   [FAIL] Close failed: " + err.msg())
        <<
    }
    os.remove(db_file + "") >>> (e) {
        <<
    }
    println("   [PASS] SQLite Pool closed cleanly.")
}

fn main() {
    println("=== SQLite Connection Pool In-Depth Verification ===\n")
    verify_sqlite_pool_deep()
    println("\n[SQLITE POOL VERIFICATION 100% COMPLETED]")
}
18_database_mysql/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@main

#database/sql
mysql #github.com/xulang-org/packages/mysql
#os

// MySQL/MariaDB:XU_MYSQL_DSN 可覆盖本地开发默认值。
// 驱动是普通 XU 包,通过 database/sql 接口使用,不依赖编译器内建。

fn main() int {
    $dsn := os.getenv("XU_MYSQL_DSN")
    if dsn.len == 0 {
        dsn = "root:root@tcp(127.0.0.1:3306)/xu_demo?tls=false"
    }

    $db := sql.open(mysql.driver(), dsn)
    db.ping() >>> (err) {
        println("MySQL ping failed")
        println(err.msg())
        << 1
    }

    db.exec("DROP TABLE IF EXISTS xu_people") >>> (err) {
        println(err.msg())
        << 1
    }
    db.exec("CREATE TABLE xu_people (id INTEGER PRIMARY KEY, name VARCHAR(100) NOT NULL, active BOOLEAN NOT NULL)") >>> (err) {
        println(err.msg())
        << 1
    }

    first := db.exec(
        "INSERT INTO xu_people (id, name, active) VALUES (?, ?, ?)",
        1,
        "Ada",
        true
    ) >>> (err) {
        println(err.msg())
        << 1
    }
    println("inserted=" + str(first.rows_affected()))

    $insert_person := db.prepare(
        "INSERT INTO xu_people (id, name, active) VALUES (?, ?, ?)"
    ) >>> (err) {
        println(err.msg())
        << 1
    }
    insert_person.exec(2, "Lin", true) >>> (err) {
        println(err.msg())
        << 1
    }
    insert_person.close()

    $tx := db.begin() >>> (err) {
        println(err.msg())
        << 1
    }
    tx.exec(
        "INSERT INTO xu_people (id, name, active) VALUES (?, ?, ?)",
        3,
        "Grace",
        true
    ) >>> (err) {
        println(err.msg())
        << 1
    }
    tx.commit() >>> (err) {
        println(err.msg())
        << 1
    }

    $rows := db.query("SELECT id, name FROM xu_people ORDER BY id") >>> (err) {
        println(err.msg())
        << 1
    }
    while rows.next() {
        values := rows.take() >>> (err) {
            println(err.msg())
            << 1
        }
        id := values[0] as int
        name := values[1] as string
        println(str(id) + ": " + name)
    }
    if rows.err().len > 0 {
        println(rows.err())
        << 1
    }
    rows.close()

    row := db.query_row("SELECT name FROM xu_people WHERE id = ?", 2)
    values := row.take() >>> (err) {
        println(err.msg())
        << 1
    }
    println("selected=" + (values[0] as string))

    db.close() >>> (err) {
        println(err.msg())
        << 1
    }
    << 0
}
17_database_postgres/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@main

#database/sql
postgres #github.com/xulang-org/packages/postgres
#os

// PostgreSQL:XU_POSTGRES_DSN 可覆盖本地开发默认值。
// 驱动是普通 XU 包,通过 database/sql 接口使用,不依赖编译器内建。

fn main() int {
    $dsn := os.getenv("XU_POSTGRES_DSN")
    if dsn.len == 0 {
        dsn = "postgres://postgres:postgres@127.0.0.1:5432/xu_demo?sslmode=disable"
    }

    $db := sql.open(postgres.driver(), dsn)
    db.ping() >>> (err) {
        println("PostgreSQL ping failed")
        println(err.msg())
        << 1
    }

    db.exec("DROP TABLE IF EXISTS xu_people") >>> (err) {
        println(err.msg())
        << 1
    }
    db.exec("CREATE TABLE xu_people (id INTEGER PRIMARY KEY, name VARCHAR(100) NOT NULL, active BOOLEAN NOT NULL)") >>> (err) {
        println(err.msg())
        << 1
    }

    first := db.exec(
        "INSERT INTO xu_people (id, name, active) VALUES ($$1, $$2, $$3)",
        1,
        "Ada",
        true
    ) >>> (err) {
        println(err.msg())
        << 1
    }
    println("inserted=" + str(first.rows_affected()))

    $insert_person := db.prepare(
        "INSERT INTO xu_people (id, name, active) VALUES ($$1, $$2, $$3)"
    )
    insert_person.exec(2, "Lin", true) >>> (err) {
        println(err.msg())
        << 1
    }
    insert_person.close()

    $tx := db.begin() >>> (err) {
        println(err.msg())
        << 1
    }
    tx.exec(
        "INSERT INTO xu_people (id, name, active) VALUES ($$1, $$2, $$3)",
        3,
        "Grace",
        true
    ) >>> (err) {
        println(err.msg())
        << 1
    }
    tx.commit() >>> (err) {
        println(err.msg())
        << 1
    }

    $rows := db.query("SELECT id, name FROM xu_people ORDER BY id") >>> (err) {
        println(err.msg())
        << 1
    }
    while rows.next() {
        values := rows.take() >>> (err) {
            println(err.msg())
            << 1
        }
        id := values[0] as int
        name := values[1] as string
        println(str(id) + ": " + name)
    }
    if rows.err().len > 0 {
        println(rows.err())
        << 1
    }
    rows.close()

    $qrows := db.query("SELECT name FROM xu_people WHERE id = $$1", 2) >>> (err) {
        println(err.msg())
        << 1
    }
    if qrows.next() {
        vals := qrows.take() >>> (err) {
            println(err.msg())
            << 1
        }
        println("selected=" + (vals[0] as string))
    }
    qrows.close()

    db.close() >>> (err) {
        println(err.msg())
        << 1
    }
    << 0
}
More related examples
  • 22_standalone_sqlite/main.xu

    Required SDK: Xu 0.8.3 · Complete project · Download source

    XuLocal SDK example
    @main
    
    #database/sql/driver
    sqlite #github.com/xulang-org/packages/sqlite
    #os
    #strings
    
    fn remove_if_exists(p string) {
        os.remove(p) >>> (err) {
            <<
        }
    }
    
    fn main() int {
        println("=== SQLite Standalone Library Integration Test ===")
    
        db_file := "/tmp/xu_sqlite_test.db"
        remove_if_exists(strings.clone(&db_file))
    
        // 1. 打开数据库连接
        conn := sqlite.open(strings.clone(&db_file))
    
        // 2. 建表
        schema := "CREATE TABLE IF NOT EXISTS developers (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INT, salary REAL, active INT);"
        res1 := conn.exec(schema, []driver.NamedValue{})
        if res1.err.len > 0 {
            println("CREATE TABLE error: " + res1.err)
            << 1
        }
        println("1. Schema created successfully.")
    
        // 3. 参数化插入记录
        $args1 := []driver.NamedValue{}
        args1.push(driver.NamedValue{name: "name", ordinal: 1, value: "Ada Lovelace"})
        args1.push(driver.NamedValue{name: "age", ordinal: 2, value: 36})
        args1.push(driver.NamedValue{name: "salary", ordinal: 3, value: 125000.5})
        args1.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
    
        res_ins1 := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", args1)
        if res_ins1.err.len > 0 {
            println("INSERT 1 error: " + res_ins1.err)
            << 1
        }
        println("2. Inserted developer 1, id=" + str(res_ins1.last_insert_id) + ", rows_affected=" + str(res_ins1.rows_affected))
    
        // 4. 第二条记录插入
        $args2 := []driver.NamedValue{}
        args2.push(driver.NamedValue{name: "name", ordinal: 1, value: "Alan Turing"})
        args2.push(driver.NamedValue{name: "age", ordinal: 2, value: 41})
        args2.push(driver.NamedValue{name: "salary", ordinal: 3, value: 140000.0})
        args2.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
    
        res_ins2 := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", args2)
        if res_ins2.err.len > 0 {
            println("INSERT 2 error: " + res_ins2.err)
            << 1
        }
        println("3. Inserted developer 2, id=" + str(res_ins2.last_insert_id) + ", rows_affected=" + str(res_ins2.rows_affected))
    
        // 5. 查询所有记录
        rows := conn.query("SELECT id, name, age, salary, active FROM developers ORDER BY id ASC;", []driver.NamedValue{})
        if rows.err.len > 0 {
            println("QUERY error: " + rows.err)
            << 1
        }
        println("4. Query returned columns count: " + str(rows.ncols))
        $col_str := "   Columns: "
        for ci in 0:rows.columns.len {
            if ci > 0 {
                col_str = col_str + ", "
            }
            col_str = col_str + rows.columns[ci]
        }
        println(col_str)
    
        nrows := rows.cells.len / rows.ncols
        println("   Total rows: " + str(nrows))
        $r := 0
        while r < nrows {
            base := r * rows.ncols
            id_str := rows.cells[base]
            name_str := rows.cells[base + 1]
            age_str := rows.cells[base + 2]
            sal_str := rows.cells[base + 3]
            act_str := rows.cells[base + 4]
            println("   Row " + str(r + 1) + ": id=" + id_str + ", name=" + name_str + ", age=" + age_str + ", salary=" + sal_str + ", active=" + act_str)
            r = r + 1
        }
    
        // 6. 带参数的查询
        $qargs := []driver.NamedValue{}
        qargs.push(driver.NamedValue{name: "name", ordinal: 1, value: "Alan Turing"})
        rows_filtered := conn.query("SELECT id, name, age FROM developers WHERE name = ?;", qargs)
        if rows_filtered.err.len > 0 {
            println("FILTERED QUERY error: " + rows_filtered.err)
            << 1
        }
        if rows_filtered.cells.len >= 3 {
            println("5. Parameterized query successful: found " + rows_filtered.cells[1] + ", age=" + rows_filtered.cells[2])
        }
    
        // 7. 更新操作
        $uargs := []driver.NamedValue{}
        uargs.push(driver.NamedValue{name: "age", ordinal: 1, value: 37})
        uargs.push(driver.NamedValue{name: "id", ordinal: 2, value: 1})
        res_up := conn.exec("UPDATE developers SET age = ? WHERE id = ?;", uargs)
        if res_up.err.len > 0 {
            println("UPDATE error: " + res_up.err)
            << 1
        }
        println("6. Updated developer 1: rows_affected=" + str(res_up.rows_affected))
    
        // 8. 事务支持
        tx := conn.begin()
        $tx_args := []driver.NamedValue{}
        tx_args.push(driver.NamedValue{name: "name", ordinal: 1, value: "Grace Hopper"})
        tx_args.push(driver.NamedValue{name: "age", ordinal: 2, value: 85})
        tx_args.push(driver.NamedValue{name: "salary", ordinal: 3, value: 160000.0})
        tx_args.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
        res_tx := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", tx_args)
        if res_tx.err.len > 0 {
            println("TX INSERT error: " + res_tx.err)
            << 1
        }
        commit_err := tx.commit()
        if commit_err.len > 0 {
            println("TX COMMIT error: " + commit_err)
            << 1
        }
        println("7. Transaction committed successfully.")
    
        // 9. 关闭与清理
        conn.close()
        remove_if_exists(db_file)
        println("=== SQLite Standalone Tests Completed Successfully! ===")
        << 0
    }

API declarations

const O_RDONLY

XuAPI
pub const O_RDONLY = 0

Purpose

Constant O_RDONLY, defining protocol or algorithm parameters.

const O_CREATE

XuAPI
pub const O_CREATE = 1

Purpose

Constant O_CREATE, defining protocol or algorithm parameters.

const O_APPEND

XuAPI
pub const O_APPEND = 2

Purpose

Constant O_APPEND, defining protocol or algorithm parameters.

const O_EXCL

XuAPI
pub const O_EXCL = 3

Purpose

Constant O_EXCL, defining protocol or algorithm parameters.

const O_LOCK

XuAPI
pub const O_LOCK = 4

Purpose

Constant O_LOCK, defining protocol or algorithm parameters.

const O_LOCK_EXISTING

XuAPI
pub const O_LOCK_EXISTING = 5

Purpose

Constant O_LOCK_EXISTING, defining protocol or algorithm parameters.

const SEEK_SET

XuAPI
pub const SEEK_SET = 0

Purpose

Constant SEEK_SET, defining protocol or algorithm parameters.

const SEEK_CUR

XuAPI
pub const SEEK_CUR = 1

Purpose

Constant SEEK_CUR, defining protocol or algorithm parameters.

const SEEK_END

XuAPI
pub const SEEK_END = 2

Purpose

Constant SEEK_END, defining protocol or algorithm parameters.

args

XuAPI
pub fn args() []string

Purpose

sys_args is the private builtin spelling. Calling args from this module would resolve to the public wrapper below and recurse.

Parameters

No arguments are required.

Returns

[]string — slice result

argv

XuAPI
pub fn argv() []string

Purpose

argv is retained as the compatibility spelling used by older Xu programs.

Parameters

No arguments are required.

Returns

[]string — slice result

read

XuAPI
pub fn read(path string) string

Purpose

read reads the file named by path and returns the contents. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

string — result string

write

XuAPI
pub fn write(path string, data string) int

Purpose

write writes data to the named file, creating it if necessary. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)
data string
data (string)

Returns

int — bytes written

struct File

XuAPI
pub struct File {

}

Purpose

Streaming file owner. Non-Copy via its compiler-managed drop hook.

Public fields

This type has no public fields.

open_lock

XuAPI
pub fn open_lock(path string, existing bool) File

Purpose

Exclusive nonblocking process lock. Closing File or process termination releases it. The stable lock file must never be unlinked while in use. o_lock creates without truncating; o_lock_existing opens read-only. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)
existing bool
existing (bool)

Returns

File — a value of this type

open

XuAPI
pub fn open(path string) File

Purpose

Open resource or establish connection. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

File — a value of this type

create

XuAPI
pub fn create(path string) File

Purpose

Create a new file, directory, or resource. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

File — a value of this type

open_append

XuAPI
pub fn open_append(path string) File

Purpose

Open append. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

File — a value of this type

create_excl

XuAPI
pub fn create_excl(path string) File

Purpose

Create excl. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

File — a value of this type

(&File) read

XuAPI
pub fn (f &File) read(max int) string

Purpose

Operation on &File: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.

Parameters

f &File
The File instance used by this call (borrow).
max int
maximum (int)

Returns

string — result string

(&File) write

XuAPI
pub fn (f &File) write(data string) int

Purpose

Operation on &File: write data and return the byte count. Throws on failure; handle the error or propagate it to the caller.

Parameters

f &File
The File instance used by this call (borrow).
data string
data (string)

Returns

int — bytes written

($&File) close

XuAPI
pub fn (f $&File) close() int

Purpose

Operation on $&File: close the resource. Throws on failure; handle the error or propagate it to the caller.

Parameters

f $&File
The File instance used by this call (exclusive borrow).

Returns

int — a value of this type

(&File) seek

XuAPI
pub fn (f &File) seek(offset int, whence int) int

Purpose

Operation on &File: move the read position. whence 0/1/2 = absolute / relative / from end. Throws on failure; handle the error or propagate it to the caller.

Parameters

f &File
The File instance used by this call (borrow).
offset int
offset (int)
whence int
seek base: 0 start / 1 current / 2 end (int)

Returns

int — a value of this type

struct Pipe

XuAPI
pub struct Pipe {
    pub r File
    pub w File
}

Purpose

Structure encapsulating r, w.

Public fields

r File
rune (Unicode code point)
w File
writer or width

pipe

XuAPI
pub fn pipe() Pipe

Purpose

Create a pair of connected synchronous in-memory pipe endpoints. Throws on failure; handle the error or propagate it to the caller.

Parameters

No arguments are required.

Returns

Pipe — a value of this type

read_dir

XuAPI
pub fn read_dir(path string) string

Purpose

sys_read_dir avoids the same-name wrapper shadowing the private builtin. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

string — result string

listdir

XuAPI
pub fn listdir(path string) string

Purpose

listdir is retained as a compatibility spelling. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

string — result string

getpid

XuAPI
pub fn getpid() int

Purpose

Execute getpid.

Parameters

No arguments are required.

Returns

int — a value of this type

getppid

XuAPI
pub fn getppid() int

Purpose

Execute getppid.

Parameters

No arguments are required.

Returns

int — a value of this type

getuid

XuAPI
pub fn getuid() int

Purpose

Execute getuid.

Parameters

No arguments are required.

Returns

int — a value of this type

getgid

XuAPI
pub fn getgid() int

Purpose

Execute getgid.

Parameters

No arguments are required.

Returns

int — a value of this type

geteuid

XuAPI
pub fn geteuid() int

Purpose

Execute geteuid.

Parameters

No arguments are required.

Returns

int — a value of this type

getegid

XuAPI
pub fn getegid() int

Purpose

Execute getegid.

Parameters

No arguments are required.

Returns

int — a value of this type

getpagesize

XuAPI
pub fn getpagesize() int

Purpose

Execute getpagesize.

Parameters

No arguments are required.

Returns

int — a value of this type

hostname

XuAPI
pub fn hostname() string

Purpose

Execute hostname.

Parameters

No arguments are required.

Returns

string — result string

getwd

XuAPI
pub fn getwd() string

Purpose

getwd returns a rooted path name corresponding to the current directory.

Parameters

No arguments are required.

Returns

string — result string

getenv

XuAPI
pub fn getenv(name string) string

Purpose

getenv retrieves the value of the environment variable named by the key.

Parameters

name string
name (string)

Returns

string — result string

See usage example

struct EnvVal

XuAPI
pub struct EnvVal {
    pub value string
    pub found bool
}

Purpose

EnvVal is os.LookupEnv's two results: found is false when unset (empty value with found true is a defined empty string).

Public fields

value string
field (string)
found bool
field (bool)

lookup_env

XuAPI
pub fn lookup_env(name string) EnvVal

Purpose

Lookup env.

Parameters

name string
name (string)

Returns

EnvVal — a value of this type

environ

XuAPI
pub fn environ() []string

Purpose

Execute environ.

Parameters

No arguments are required.

Returns

[]string — slice result

temp_dir

XuAPI
pub fn temp_dir() string

Purpose

Temp directory.

Parameters

No arguments are required.

Returns

string — result string

home_dir

XuAPI
pub fn home_dir() string

Purpose

Home directory.

Parameters

No arguments are required.

Returns

string — result string

user_home_dir

XuAPI
pub fn user_home_dir() string

Purpose

User home directory. Throws on failure; handle the error or propagate it to the caller.

Parameters

No arguments are required.

Returns

string — result string

path_separator

XuAPI
pub fn path_separator() int

Purpose

Path separator.

Parameters

No arguments are required.

Returns

int — a value of this type

is_path_separator

XuAPI
pub fn is_path_separator(c int) bool

Purpose

Whether it is path separator.

Parameters

c int
byte or character code (int)

Returns

bool — true or false

path_list_separator

XuAPI
pub fn path_list_separator() int

Purpose

path_list_separator is os.PathListSeparator: ';' on Windows, ':' elsewhere.

Parameters

No arguments are required.

Returns

int — a value of this type

dev_null

XuAPI
pub fn dev_null() string

Purpose

dev_null is os.DevNull: "NUL" on Windows, "/dev/null" elsewhere.

Parameters

No arguments are required.

Returns

string — result string

user_cache_dir

XuAPI
pub fn user_cache_dir() string

Purpose

User cache directory. Throws on failure; handle the error or propagate it to the caller.

Parameters

No arguments are required.

Returns

string — result string

user_config_dir

XuAPI
pub fn user_config_dir() string

Purpose

User config directory. Throws on failure; handle the error or propagate it to the caller.

Parameters

No arguments are required.

Returns

string — result string

stat

XuAPI
pub fn stat(path string) FileInfo

Purpose

Read metadata. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

FileInfo — a value of this type

mkdir

XuAPI
pub fn mkdir(path string) int

Purpose

Create a directory. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

int — a value of this type

rename

XuAPI
pub fn rename(old string, new string) int

Purpose

Rename. Throws on failure; handle the error or propagate it to the caller.

Parameters

old string
old value / piece to replace (string)
new string
new value / replacement (string)

Returns

int — a value of this type

remove

XuAPI
pub fn remove(path string) int

Purpose

Remove. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

int — a value of this type

See usage example

chmod

XuAPI
pub fn chmod(path string, mode int) int

Purpose

Execute chmod. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)
mode int
mode (int)

Returns

int — a value of this type

chdir

XuAPI
pub fn chdir(path string) int

Purpose

Execute chdir. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)

Returns

int — a value of this type

is_dir

XuAPI
pub fn is_dir(path string) bool

Purpose

Whether it is a directory.

Parameters

path string
filesystem path (string)

Returns

bool — true or false

is_not_exist

XuAPI
pub fn is_not_exist(msg string) bool

Purpose

Whether it is not exist.

Parameters

msg string
message (string)

Returns

bool — true or false

is_exist

XuAPI
pub fn is_exist(msg string) bool

Purpose

Whether it is exist.

Parameters

msg string
message (string)

Returns

bool — true or false

is_permission

XuAPI
pub fn is_permission(msg string) bool

Purpose

Whether it is permission.

Parameters

msg string
message (string)

Returns

bool — true or false

is_timeout

XuAPI
pub fn is_timeout(msg string) bool

Purpose

Whether it is timeout.

Parameters

msg string
message (string)

Returns

bool — true or false

struct DirEntry

XuAPI
pub struct DirEntry {
    pub name string
    pub is_dir bool
}

Purpose

Structure encapsulating name, is_dir.

Public fields

name string
name
is_dir bool
field (bool)

entries

XuAPI
pub fn entries(path0 string) []DirEntry

Purpose

Execute entries. Throws on failure; handle the error or propagate it to the caller.

Parameters

path0 string
path0 (string)

Returns

[]DirEntry — slice result

mkdir_all

XuAPI
pub fn mkdir_all(path0 string) int

Purpose

Mkdir all. Throws on failure; handle the error or propagate it to the caller.

Parameters

path0 string
path0 (string)

Returns

int — a value of this type

mkdir_temp

XuAPI
pub fn mkdir_temp(dir string, pattern string) string

Purpose

Mkdir temp. Throws on failure; handle the error or propagate it to the caller.

Parameters

dir string
directory (string)
pattern string
pattern (string)

Returns

string — result string

create_temp

XuAPI
pub fn create_temp(dir string, pattern string) string

Purpose

Create temp. Throws on failure; handle the error or propagate it to the caller.

Parameters

dir string
directory (string)
pattern string
pattern (string)

Returns

string — result string

remove_all

XuAPI
pub fn remove_all(path0 string) int

Purpose

Remove all.

Parameters

path0 string
path0 (string)

Returns

int — a value of this type

expand_env

XuAPI
pub fn expand_env(s &string) string

Purpose

Expand env.

Parameters

s &string
source string (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

closed_file

XuAPI
pub fn closed_file() File

Purpose

A closed owner for aggregate initialization; no resource is acquired.

Parameters

No arguments are required.

Returns

File — a value of this type

($&File) take

XuAPI
pub fn (f $&File) take() File

Purpose

Transfer a field-owned File without cloning or moving through a borrow. Throws on failure; handle the error or propagate it to the caller.

Parameters

f $&File
The File instance used by this call (exclusive borrow).

Returns

File — a value of this type

($&File) release_descriptor

XuAPI
pub fn (f $&File) release_descriptor() int

Purpose

Explicit ownership handoff to a raw platform adapter. The adapter must release the returned descriptor exactly once; this owner becomes closed. Throws on failure; handle the error or propagate it to the caller.

Parameters

f $&File
The File instance used by this call (exclusive borrow).

Returns

int — a value of this type

(&File) descriptor

XuAPI
pub fn (f &File) descriptor() int

Purpose

Borrowed raw descriptor for platform adapters. It does not transfer ownership callers must not close it or manufacture another owner from it. Throws on failure; handle the error or propagate it to the caller.

Parameters

f &File
The File instance used by this call (borrow).

Returns

int — a value of this type

struct DirFS

XuAPI
pub struct DirFS {
    pub dir string
}

Purpose

Structure encapsulating dir.

Public fields

dir string
directory

dir_fs

XuAPI
pub fn dir_fs(dir string) DirFS

Purpose

Directory fs.

Parameters

dir string
directory (string)

Returns

DirFS — a value of this type

(&DirFS) open

XuAPI
pub fn (d &DirFS) open(name string) File

Purpose

Operation on &DirFS: open resource or establish connection. Throws on failure; handle the error or propagate it to the caller.

Parameters

d &DirFS
The DirFS instance used by this call (borrow).
name string
name (string)

Returns

File — a value of this type

(&DirFS) stat

XuAPI
pub fn (d &DirFS) stat(name string) FileInfo

Purpose

Operation on &DirFS: read metadata. Throws on failure; handle the error or propagate it to the caller.

Parameters

d &DirFS
The DirFS instance used by this call (borrow).
name string
name (string)

Returns

FileInfo — a value of this type

(&DirFS) read_dir

XuAPI
pub fn (d &DirFS) read_dir(name string) []DirEntry

Purpose

Operation on &DirFS: read directory. Throws on failure; handle the error or propagate it to the caller.

Parameters

d &DirFS
The DirFS instance used by this call (borrow).
name string
name (string)

Returns

[]DirEntry — slice result

(&DirFS) read_file

XuAPI
pub fn (d &DirFS) read_file(name string) string

Purpose

Operation on &DirFS: read file. Throws on failure; handle the error or propagate it to the caller.

Parameters

d &DirFS
The DirFS instance used by this call (borrow).
name string
name (string)

Returns

string — result string

(&DirFS) sub

XuAPI
pub fn (d &DirFS) sub(name string) DirFS

Purpose

Operation on &DirFS: subtract. Throws on failure; handle the error or propagate it to the caller.

Parameters

d &DirFS
The DirFS instance used by this call (borrow).
name string
name (string)

Returns

DirFS — a value of this type

(&File) read_result

XuAPI
pub fn (f &File) read_result(n int) io.ReadResult

Purpose

Operation on &File: read result.

Parameters

f &File
The File instance used by this call (borrow).
n int
count, length, or limit (int)

Returns

io.ReadResult — a value of this type

(&File) write_result

XuAPI
pub fn (f &File) write_result(data string) io.WriteResult

Purpose

Operation on &File: write result.

Parameters

f &File
The File instance used by this call (borrow).
data string
data (string)

Returns

io.WriteResult — a value of this type

socket

XuAPI
pub fn socket(domain int, typ int, proto int) int

Purpose

Execute socket. Throws on failure; handle the error or propagate it to the caller.

Parameters

domain int
domain (int)
typ int
typ (int)
proto int
proto (int)

Returns

int — a value of this type

bind4

XuAPI
pub fn bind4(fd int, a int, b int, c int, d int, port int) int

Purpose

Execute bind4. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
a int
value (int)
b int
b (int)
c int
byte or character code (int)
d int
d (int)
port int
port (int)

Returns

int — a value of this type

connect4

XuAPI
pub fn connect4(fd int, a int, b int, c int, d int, port int) int

Purpose

Execute connect4. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
a int
value (int)
b int
b (int)
c int
byte or character code (int)
d int
d (int)
port int
port (int)

Returns

int — a value of this type

bind6

XuAPI
pub fn bind6(fd int, ip string, port int) int

Purpose

IPv6 socket address helpers. ip is the 16-byte raw IPv6 value used by net.IP; the runtime validates its length before touching the host socket. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
ip string
ip (string)
port int
port (int)

Returns

int — a value of this type

connect6

XuAPI
pub fn connect6(fd int, ip string, port int) int

Purpose

Execute connect6. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
ip string
ip (string)
port int
port (int)

Returns

int — a value of this type

bind_unix

XuAPI
pub fn bind_unix(fd int, path string) int

Purpose

Unix-domain pathname sockets. These calls are available on Darwin/Linux the hosted runtime returns an explicit unsupported error on Windows. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
path string
filesystem path (string)

Returns

int — a value of this type

connect_unix

XuAPI
pub fn connect_unix(fd int, path string) int

Purpose

Connect unix. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
path string
filesystem path (string)

Returns

int — a value of this type

listen

XuAPI
pub fn listen(fd int, backlog int) int

Purpose

Listen for connections. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
backlog int
backlog (int)

Returns

int — a value of this type

accept

XuAPI
pub fn accept(fd int) int

Purpose

Accept one connection. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)

Returns

int — a value of this type

getsockname

XuAPI
pub fn getsockname(fd int) string

Purpose

Execute getsockname. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)

Returns

string — result string

getpeername

XuAPI
pub fn getpeername(fd int) string

Purpose

Execute getpeername. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)

Returns

string — result string

shutdown

XuAPI
pub fn shutdown(fd int, how int) int

Purpose

Execute shutdown. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
how int
how (int)

Returns

int — a value of this type

sendto

XuAPI
pub fn sendto(fd int, addr string, data string) int

Purpose

Execute sendto. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
addr string
address (string)
data string
data (string)

Returns

int — a value of this type

recvfrom

XuAPI
pub fn recvfrom(fd int, max int) string

Purpose

Execute recvfrom. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
max int
maximum (int)

Returns

string — result string

fd_read

XuAPI
pub fn fd_read(fd int, max int) string

Purpose

Fd read. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
max int
maximum (int)

Returns

string — result string

fd_write

XuAPI
pub fn fd_write(fd int, data string) int

Purpose

Fd write. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
data string
data (string)

Returns

int — a value of this type

fd_close

XuAPI
pub fn fd_close(fd int) int

Purpose

Fd close. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)

Returns

int — a value of this type

socket_read

XuAPI
pub fn socket_read(fd int, max int) string

Purpose

Socket-specific I/O. Generic fd_* maps to file handles on Windows Winsock SOCKETs must instead use recv/send/closesocket. Keep these wrappers private to the net package's higher-level Conn/PacketConn paths. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
max int
maximum (int)

Returns

string — result string

socket_write

XuAPI
pub fn socket_write(fd int, data string) int

Purpose

Socket write. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
data string
data (string)

Returns

int — a value of this type

socket_close

XuAPI
pub fn socket_close(fd int) int

Purpose

Socket close. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)

Returns

int — a value of this type

socket_poll

XuAPI
pub fn socket_poll(fd int, events int) int

Purpose

socket_poll checks readiness immediately. events is 1 for read/accept or 4 for write. Returns 0 when pending and 1 when ready (including EOF). The descriptor's blocking mode is unchanged. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
events int
events (int)

Returns

int — a value of this type

set_sock_read_timeout

XuAPI
pub fn set_sock_read_timeout(fd int, ns int) int

Purpose

Set sock read timeout. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
ns int
ns (int)

Returns

int — a value of this type

set_sock_write_timeout

XuAPI
pub fn set_sock_write_timeout(fd int, ns int) int

Purpose

Set sock write timeout. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
ns int
ns (int)

Returns

int — a value of this type

set_sock_timeout

XuAPI
pub fn set_sock_timeout(fd int, ns int) int

Purpose

set_sock_timeout sets SO_SNDTIMEO and SO_RCVTIMEO. ns is relative 0 clears. connect honors it on Darwin; Linux connect still uses SYN retries. read/write honor it on both. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
ns int
ns (int)

Returns

int — a value of this type

setsockopt

XuAPI
pub fn setsockopt(fd int, level int, optname int, value string) int

Purpose

setsockopt passes value as the raw option bytes. level and optname use the host numbering (Darwin, Linux and Winsock differ); net keeps those tables. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
level int
level (int)
optname int
optname (int)
value string
value (string)

Returns

int — a value of this type

setsockopt_int

XuAPI
pub fn setsockopt_int(fd int, level int, optname int, value int) int

Purpose

setsockopt_int encodes value as a native 32-bit little-endian C int. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
level int
level (int)
optname int
optname (int)
value int
value (int)

Returns

int — a value of this type

socket_wait

XuAPI
pub fn socket_wait(fd int, events int, timeout_ns int) int

Purpose

socket_wait waits for non-consuming readiness. events uses read=1 and write=2; the runtime normalizes error=4 and hangup=8 across platforms. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
events int
events (int)
timeout_ns int
timeout in nanoseconds (int)

Returns

int — a value of this type

fd_wait_read

XuAPI
pub fn fd_wait_read(fd int, timeout_ns int) int

Purpose

fd_wait_read waits for file/pipe readability without consuming bytes or changing blocking mode. Returns 1 for data/EOF and 0 for a timeout. timeout_ns uses 0=infinite, negative=immediate. The caller must own reads from fd; readiness does not reserve bytes against another reader. POSIX poll accepts regular files, unlike Linux epoll. Windows probes pipe HANDLEs (0/1/2 map to standard handles) and treats disks as ready. Console signals can include non-key events, so complete console lines are not guaranteed. Polling has millisecond sleep granularity, not sub-ms latency no overlapped I/O or background reader remains after a timeout. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
timeout_ns int
timeout in nanoseconds (int)

Returns

int — a value of this type

socket_wait_read

XuAPI
pub fn socket_wait_read(fd int, timeout_ns int) int

Purpose

socket_wait_read uses kernel poll (or task_poll_wait when peer tasks exist). Returns 1 for data/EOF, 0 for timeout; timeout_ns uses 0=infinite and negative=immediate. Poll errors propagate. The caller must own reads. Throws on failure; handle the error or propagate it to the caller.

Parameters

fd int
file descriptor (int)
timeout_ns int
timeout in nanoseconds (int)

Returns

int — a value of this type

interface_data

XuAPI
pub fn interface_data(name string) string

Purpose

interface_data returns the host network-interface snapshot used by the net package. Passing an empty name returns NUL-separated interface names passing a name returns a fixed binary metadata record. Platform ABI structs stay behind this os/runtime boundary. Throws on failure; handle the error or propagate it to the caller.

Parameters

name string
name (string)

Returns

string — result string

(&File) read_n

XuAPI
pub fn (f &File) read_n(n int) string

Purpose

io.StreamReader/StreamWriter adapters. These use the existing descriptor callers retain responsibility for close and may continue using File APIs. Throws on failure; handle the error or propagate it to the caller.

Parameters

f &File
The File instance used by this call (borrow).
n int
count, length, or limit (int)

Returns

string — result string

(&File) write_str

XuAPI
pub fn (f &File) write_str(data string) int

Purpose

Operation on &File: write a string. Throws on failure; handle the error or propagate it to the caller.

Parameters

f &File
The File instance used by this call (borrow).
data string
data (string)

Returns

int — a value of this type