XU XUv0.16.0

#os

导入方式: #os

公开 API: 84 函数, 16 方法, 5 类型, 9 常量, 0 方法集

进程、文件、环境。hosted 下 File:open/create/read/write/close。getwd 失败返空串。

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

36_test_sqlite_pool/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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
}
更多相关示例
  • 22_standalone_sqlite/main.xu

    所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

    Xu本地 SDK 示例
    @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 声明

const O_RDONLY

XuAPI
pub const O_RDONLY = 0

用途

常量 O_RDONLY,定义了相关的协议或算法配置参数。

const O_CREATE

XuAPI
pub const O_CREATE = 1

用途

常量 O_CREATE,定义了相关的协议或算法配置参数。

const O_APPEND

XuAPI
pub const O_APPEND = 2

用途

常量 O_APPEND,定义了相关的协议或算法配置参数。

const O_EXCL

XuAPI
pub const O_EXCL = 3

用途

常量 O_EXCL,定义了相关的协议或算法配置参数。

const O_LOCK

XuAPI
pub const O_LOCK = 4

用途

常量 O_LOCK,定义了相关的协议或算法配置参数。

const O_LOCK_EXISTING

XuAPI
pub const O_LOCK_EXISTING = 5

用途

常量 O_LOCK_EXISTING,定义了相关的协议或算法配置参数。

const SEEK_SET

XuAPI
pub const SEEK_SET = 0

用途

常量 SEEK_SET,定义了相关的协议或算法配置参数。

const SEEK_CUR

XuAPI
pub const SEEK_CUR = 1

用途

常量 SEEK_CUR,定义了相关的协议或算法配置参数。

const SEEK_END

XuAPI
pub const SEEK_END = 2

用途

常量 SEEK_END,定义了相关的协议或算法配置参数。

args

XuAPI
pub fn args() []string

用途

获取当前程序的命令行参数切片(从程序名开始)。

参数

无需传入参数。

返回值

[]string — 切片结果

argv

XuAPI
pub fn argv() []string

用途

命令行参数。

参数

无需传入参数。

返回值

[]string — 切片结果

read

XuAPI
pub fn read(path string) string

用途

读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

string — 结果字符串

write

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

用途

写入数据,返回写入的字节数。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)
data string
数据(string)

返回值

int — 写入的字节数

struct File

XuAPI
pub struct File {

}

用途

File 上下文或状态结构体。

公开字段

此类型没有公开字段。

open_lock

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

用途

打开加锁。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)
existing bool
existing(bool)

返回值

File — 该类型的值

open

XuAPI
pub fn open(path string) File

用途

打开资源或建立网络连接。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

File — 该类型的值

create

XuAPI
pub fn create(path string) File

用途

创建新的文件、目录或资源。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

File — 该类型的值

open_append

XuAPI
pub fn open_append(path string) File

用途

打开追加。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

File — 该类型的值

create_excl

XuAPI
pub fn create_excl(path string) File

用途

执行 create excl 操作。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

File — 该类型的值

(&File) read

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

用途

&File 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。
max int
上限(int)

返回值

string — 结果字符串

(&File) write

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

用途

&File 上的操作:写入数据,返回写入的字节数。失败时抛出错误,调用方需处理或继续传播。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。
data string
数据(string)

返回值

int — 写入的字节数

($&File) close

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

用途

$&File 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。

参数

f $&File
本次调用使用的 File 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

(&File) seek

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

用途

&File 上的操作:按 whence 把读位置移到 offset。whence 0/1/2 分别为绝对、相对、从末尾。失败时抛出错误,调用方需处理或继续传播。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。
offset int
偏移(int)
whence int
定位基准:0 起点 / 1 当前 / 2 末尾(int)

返回值

int — 该类型的值

struct Pipe

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

用途

承载 r、w 等属性的结构体。

公开字段

r File
rune(Unicode 码点)
w File
写入侧 / 宽度

pipe

XuAPI
pub fn pipe() Pipe

用途

创建一对连接在一起的同步内存管道端点。失败时抛出错误,调用方需处理或继续传播。

参数

无需传入参数。

返回值

Pipe — 该类型的值

read_dir

XuAPI
pub fn read_dir(path string) string

用途

列目录。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

string — 结果字符串

listdir

XuAPI
pub fn listdir(path string) string

用途

列出目录(文本)。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

string — 结果字符串

getpid

XuAPI
pub fn getpid() int

用途

进程 id。

参数

无需传入参数。

返回值

int — 该类型的值

getppid

XuAPI
pub fn getppid() int

用途

父进程 id。

参数

无需传入参数。

返回值

int — 该类型的值

getuid

XuAPI
pub fn getuid() int

用途

用户 id。

参数

无需传入参数。

返回值

int — 该类型的值

getgid

XuAPI
pub fn getgid() int

用途

组 id。

参数

无需传入参数。

返回值

int — 该类型的值

geteuid

XuAPI
pub fn geteuid() int

用途

有效用户 id。

参数

无需传入参数。

返回值

int — 该类型的值

getegid

XuAPI
pub fn getegid() int

用途

有效组 id。

参数

无需传入参数。

返回值

int — 该类型的值

getpagesize

XuAPI
pub fn getpagesize() int

用途

页大小。

参数

无需传入参数。

返回值

int — 该类型的值

hostname

XuAPI
pub fn hostname() string

用途

主机名。

参数

无需传入参数。

返回值

string — 结果字符串

getwd

XuAPI
pub fn getwd() string

用途

工作目录。

参数

无需传入参数。

返回值

string — 结果字符串

getenv

XuAPI
pub fn getenv(name string) string

用途

读环境变量。

参数

name string
名字(string)

返回值

string — 结果字符串

查看用法示例

struct EnvVal

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

用途

承载 value、found 等属性的结构体。

公开字段

value string
字段(string)
found bool
字段(bool)

lookup_env

XuAPI
pub fn lookup_env(name string) EnvVal

用途

执行 lookup env 操作。

参数

name string
名字(string)

返回值

EnvVal — 该类型的值

environ

XuAPI
pub fn environ() []string

用途

环境变量表。

参数

无需传入参数。

返回值

[]string — 切片结果

temp_dir

XuAPI
pub fn temp_dir() string

用途

临时目录。

参数

无需传入参数。

返回值

string — 结果字符串

home_dir

XuAPI
pub fn home_dir() string

用途

家目录。

参数

无需传入参数。

返回值

string — 结果字符串

user_home_dir

XuAPI
pub fn user_home_dir() string

用途

用户家目录目录部分。失败时抛出错误,调用方需处理或继续传播。

参数

无需传入参数。

返回值

string — 结果字符串

path_separator

XuAPI
pub fn path_separator() int

用途

路径分隔符。

参数

无需传入参数。

返回值

int — 该类型的值

is_path_separator

XuAPI
pub fn is_path_separator(c int) bool

用途

是否路径分隔符。

参数

c int
字节或字符码(int)

返回值

bool — 是否成立

path_list_separator

XuAPI
pub fn path_list_separator() int

用途

当前操作系统 PATH 环境变量的分隔符(Unix 为 ':',Windows 为 ';')。

参数

无需传入参数。

返回值

int — 该类型的值

dev_null

XuAPI
pub fn dev_null() string

用途

操作系统空设备文件路径(Unix 为 '/dev/null',Windows 为 'NUL')。

参数

无需传入参数。

返回值

string — 结果字符串

user_cache_dir

XuAPI
pub fn user_cache_dir() string

用途

用户缓存目录。失败时抛出错误,调用方需处理或继续传播。

参数

无需传入参数。

返回值

string — 结果字符串

user_config_dir

XuAPI
pub fn user_config_dir() string

用途

用户配置目录。失败时抛出错误,调用方需处理或继续传播。

参数

无需传入参数。

返回值

string — 结果字符串

stat

XuAPI
pub fn stat(path string) FileInfo

用途

读取元信息。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

FileInfo — 该类型的值

mkdir

XuAPI
pub fn mkdir(path string) int

用途

创建目录。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

int — 该类型的值

rename

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

用途

重命名。失败时抛出错误,调用方需处理或继续传播。

参数

old string
旧值 / 被替换片段(string)
new string
新值 / 替换成的片段(string)

返回值

int — 该类型的值

remove

XuAPI
pub fn remove(path string) int

用途

删除。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

int — 该类型的值

查看用法示例

chmod

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

用途

改权限。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)
mode int
模式(int)

返回值

int — 该类型的值

chdir

XuAPI
pub fn chdir(path string) int

用途

改目录。失败时抛出错误,调用方需处理或继续传播。

参数

path string
文件系统路径(string)

返回值

int — 该类型的值

is_dir

XuAPI
pub fn is_dir(path string) bool

用途

是否为目录。

参数

path string
文件系统路径(string)

返回值

bool — 是否成立

is_not_exist

XuAPI
pub fn is_not_exist(msg string) bool

用途

是否不存在类错误。

参数

msg string
消息(string)

返回值

bool — 是否成立

is_exist

XuAPI
pub fn is_exist(msg string) bool

用途

是否已存在类错误。

参数

msg string
消息(string)

返回值

bool — 是否成立

is_permission

XuAPI
pub fn is_permission(msg string) bool

用途

判断是否 permission。

参数

msg string
消息(string)

返回值

bool — 是否成立

is_timeout

XuAPI
pub fn is_timeout(msg string) bool

用途

判断是否 timeout。

参数

msg string
消息(string)

返回值

bool — 是否成立

struct DirEntry

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

用途

承载 name、is_dir 等属性的结构体。

公开字段

name string
名字
is_dir bool
字段(bool)

entries

XuAPI
pub fn entries(path0 string) []DirEntry

用途

列出目录项。失败时抛出错误,调用方需处理或继续传播。

参数

path0 string
path0(string)

返回值

[]DirEntry — 切片结果

mkdir_all

XuAPI
pub fn mkdir_all(path0 string) int

用途

递归建目录。失败时抛出错误,调用方需处理或继续传播。

参数

path0 string
path0(string)

返回值

int — 该类型的值

mkdir_temp

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

用途

建临时目录。失败时抛出错误,调用方需处理或继续传播。

参数

dir string
目录(string)
pattern string
匹配模式(string)

返回值

string — 结果字符串

create_temp

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

用途

建临时文件。失败时抛出错误,调用方需处理或继续传播。

参数

dir string
目录(string)
pattern string
匹配模式(string)

返回值

string — 结果字符串

remove_all

XuAPI
pub fn remove_all(path0 string) int

用途

递归删除。

参数

path0 string
path0(string)

返回值

int — 该类型的值

expand_env

XuAPI
pub fn expand_env(s &string) string

用途

展开 $VAR。

参数

s &string
源字符串(只读借用,调用方仍持有原值;类型 &string)

返回值

string — 结果字符串

closed_file

XuAPI
pub fn closed_file() File

用途

执行 closed file 操作。

参数

无需传入参数。

返回值

File — 该类型的值

($&File) take

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

用途

在 $&File 上执行 take 操作。失败时抛出错误,调用方需处理或继续传播。

参数

f $&File
本次调用使用的 File 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

File — 该类型的值

($&File) release_descriptor

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

用途

$&File 上的操作:执行 release descriptor 操作。失败时抛出错误,调用方需处理或继续传播。

参数

f $&File
本次调用使用的 File 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

(&File) descriptor

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

用途

获取底层持有的操作系统原生套接字或文件描述符(fd)。失败时抛出错误,调用方需处理或继续传播。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

struct DirFS

XuAPI
pub struct DirFS {
    pub dir string
}

用途

承载 dir 等属性的结构体。

公开字段

dir string
目录

dir_fs

XuAPI
pub fn dir_fs(dir string) DirFS

用途

目录文件系统。

参数

dir string
目录(string)

返回值

DirFS — 该类型的值

(&DirFS) open

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

用途

&DirFS 上的操作:打开资源或建立网络连接。失败时抛出错误,调用方需处理或继续传播。

参数

d &DirFS
本次调用使用的 DirFS 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

File — 该类型的值

(&DirFS) stat

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

用途

&DirFS 上的操作:读取元信息。失败时抛出错误,调用方需处理或继续传播。

参数

d &DirFS
本次调用使用的 DirFS 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

FileInfo — 该类型的值

(&DirFS) read_dir

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

用途

&DirFS 上的操作:列目录。失败时抛出错误,调用方需处理或继续传播。

参数

d &DirFS
本次调用使用的 DirFS 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

[]DirEntry — 切片结果

(&DirFS) read_file

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

用途

&DirFS 上的操作:读文件。失败时抛出错误,调用方需处理或继续传播。

参数

d &DirFS
本次调用使用的 DirFS 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

string — 结果字符串

(&DirFS) sub

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

用途

&DirFS 上的操作:相减。失败时抛出错误,调用方需处理或继续传播。

参数

d &DirFS
本次调用使用的 DirFS 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

DirFS — 该类型的值

(&File) read_result

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

用途

&File 上的操作:读取结果。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。
n int
数量、长度或上限(int)

返回值

io.ReadResult — 该类型的值

(&File) write_result

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

用途

&File 上的操作:写入结果。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。
data string
数据(string)

返回值

io.WriteResult — 该类型的值

socket

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

用途

套接字。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

int — 该类型的值

bind4

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

用途

IPv4 bind。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
a int
值(int)
b int
b(int)
c int
字节或字符码(int)
d int
d(int)
port int
端口(int)

返回值

int — 该类型的值

connect4

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

用途

IPv4 connect。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
a int
值(int)
b int
b(int)
c int
字节或字符码(int)
d int
d(int)
port int
端口(int)

返回值

int — 该类型的值

bind6

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

用途

执行 bind6 处理。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
ip string
ip(string)
port int
端口(int)

返回值

int — 该类型的值

connect6

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

用途

执行 connect6 处理。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
ip string
ip(string)
port int
端口(int)

返回值

int — 该类型的值

bind_unix

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

用途

执行 bind unix 操作。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
path string
文件系统路径(string)

返回值

int — 该类型的值

connect_unix

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

用途

执行 connect unix 操作。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
path string
文件系统路径(string)

返回值

int — 该类型的值

listen

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

用途

开始监听。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
backlog int
backlog(int)

返回值

int — 该类型的值

accept

XuAPI
pub fn accept(fd int) int

用途

接受一个连接。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)

返回值

int — 该类型的值

getsockname

XuAPI
pub fn getsockname(fd int) string

用途

执行 getsockname 处理。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)

返回值

string — 结果字符串

getpeername

XuAPI
pub fn getpeername(fd int) string

用途

执行 getpeername 处理。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)

返回值

string — 结果字符串

shutdown

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

用途

执行 shutdown 处理。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
how int
how(int)

返回值

int — 该类型的值

sendto

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

用途

发送数据报。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
addr string
地址(string)
data string
数据(string)

返回值

int — 该类型的值

recvfrom

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

用途

收数据报。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
max int
上限(int)

返回值

string — 结果字符串

fd_read

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

用途

从 fd 读。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
max int
上限(int)

返回值

string — 结果字符串

fd_write

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

用途

向 fd 写。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
data string
数据(string)

返回值

int — 该类型的值

fd_close

XuAPI
pub fn fd_close(fd int) int

用途

关 fd。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)

返回值

int — 该类型的值

socket_read

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

用途

读取套接字。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
max int
上限(int)

返回值

string — 结果字符串

socket_write

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

用途

写入套接字。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
data string
数据(string)

返回值

int — 该类型的值

socket_close

XuAPI
pub fn socket_close(fd int) int

用途

关闭套接字。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)

返回值

int — 该类型的值

socket_poll

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

用途

执行 socket poll 操作。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
events int
events(int)

返回值

int — 该类型的值

set_sock_read_timeout

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

用途

设置 sock read timeout。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
ns int
ns(int)

返回值

int — 该类型的值

set_sock_write_timeout

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

用途

设置 sock write timeout。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
ns int
ns(int)

返回值

int — 该类型的值

set_sock_timeout

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

用途

设置 sock timeout。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
ns int
ns(int)

返回值

int — 该类型的值

setsockopt

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

用途

执行 setsockopt 处理。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
level int
level(int)
optname int
optname(int)
value string
value(string)

返回值

int — 该类型的值

setsockopt_int

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

用途

执行 setsockopt int 操作。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
level int
level(int)
optname int
optname(int)
value int
value(int)

返回值

int — 该类型的值

socket_wait

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

用途

套接字等待。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
events int
events(int)
timeout_ns int
超时(纳秒)(int)

返回值

int — 该类型的值

fd_wait_read

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

用途

读取fd等待。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
timeout_ns int
超时(纳秒)(int)

返回值

int — 该类型的值

socket_wait_read

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

用途

读取套接字等待。失败时抛出错误,调用方需处理或继续传播。

参数

fd int
文件描述符(int)
timeout_ns int
超时(纳秒)(int)

返回值

int — 该类型的值

interface_data

XuAPI
pub fn interface_data(name string) string

用途

执行 interface data 操作。失败时抛出错误,调用方需处理或继续传播。

参数

name string
名字(string)

返回值

string — 结果字符串

(&File) read_n

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

用途

&File 上的操作:最多读 n 字节;到末尾可返回更短或空串。失败时抛出错误,调用方需处理或继续传播。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。
n int
数量、长度或上限(int)

返回值

string — 结果字符串

(&File) write_str

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

用途

&File 上的操作:写入字符串。失败时抛出错误,调用方需处理或继续传播。

参数

f &File
本次调用使用的 File 实例;以只读借用传入,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值