标准库与运算符参考
xxc 标准库与运算符参考
> 全部模块随编译器单文件分发,无需安装。*(async; concurrency)* 标注的
> 模块参与异步 I/O 运行时(--features concurrency);db/sqlite 需要
> sqlite 特性。相关页:语言指南 ·
> English: Standard Library Reference。
标准库
std/time
time.now() 毫秒级 Unix 时间戳
time.epoch() 秒级 Unix 时间戳
time.sleep(ms) 阻塞当前线程 *(不感知任务)*
std/json
json.encode(v) -> string(支持 struct/class/array/map)
json.decode(s) -> value(对象可索引访问)
std/random
rand.int(max) [0, max)
rand.float() [0, 1)
rand.seed(n)
rand.choice(arr)
rand.shuffle(arr) -> 新的乱序数组
std/os
os.file_exists(path) -> bool
os.read_file(path) -> string
os.write_file(path, content)
os.mkdir(path)
os.list_dir(path) -> 名称数组
os.env(key) -> string 或 nil
os.cwd() -> string
std/regex
re.matches(pat, text) -> bool
re.find(pat, text) -> 首个匹配串或 nil
re.find_all(pat, text) -> 全部匹配数组
re.replace(pat, text, repl) -> string
re.split(pat, text) -> array
支持语法:字面量、.、* + ? {n,m}、字符类 [abc] [^abc]
[a-z]、\d \w \s(含否定)、^ $ 锚、|、(...)、转义。
std/crypto
crypto.md5(s) -> 32 位十六进制摘要
crypto.sha256(s) -> 64 位十六进制摘要
std/base64
b64.encode(s) b64.decode(s)
std/uri
u := uri.parse("https://host:8080/path?q=1")
u.scheme u.host u.port u.path u.query
uri.encode(s) 百分号编码
std/fs *(async; concurrency)*
fs.read_file(path) fs.write_file(path, content)
net/tcp *(async; concurrency)*
l := tcp.listen(port)
c := tcp.accept(l)
line := tcp.read_line(c) // 读到并含 '\n'
body := tcp.read(c, n) // 恰好 n 字节
tcp.write(c, s)
tcp.close(c)
net/http *(async; concurrency)*
body := http.get("http://host/path")
db/sqlite *(特性 sqlite)*
db := sqlite.open(path) // ":memory:" 可用
sqlite.exec(db, sql) // DDL / DML
rows := sqlite.query(db, sql) // -> 行对象数组
sqlite.close(db)
for row in rows { print(row.id, row.name) }
gui(无头)
root := gui.col([gui.text("Hi"), gui.rect(60, 20, gui.rgb(40,120,200))])
gui.render(root, "out.svg", w, h)
gui.render_bmp(root, "out.bmp", w, h)
gui.row([...]) gui.text(s) gui.rect(w,h,color) gui.rgb(r,g,b)
gui.show(root, w, h) // 原生窗口(特性 gui-window)
js —— 内置 JavaScript 引擎
js.run(code) // 执行,console.log 输出
js.eval(code) // 执行并返回末值(已转换)
引擎实现 ES 子集:let/const、函数与箭头函数、对象、数组、闭包、
控制流、try/catch、Math、JSON、console。
webview
wv.render(html, w, h, "out.bmp") // 解析 + 排版 + 光栅化
wv.run_scripts(html) // 执行 <script> 标签
运算符
优先级(高到低):
| 级 | 运算符 | 类别 |
|---|---|---|
| 最高 | fn(...) 调用、. 字段/方法、[...] 索引、后缀 ? | 后缀 |
| — | 一元 - ! ~ | 一元 |
| 7 | * / % | 乘除 |
| 6 | + - | 加减 |
| 5 | << >> & \| ^ | 位运算 |
| 4 | < <= > >= | 比较 |
| 3 | == != | 相等 |
| 2 | && | 逻辑与 |
| 1 | \|\| | 逻辑或 |
| 0 | =(赋值)、ch <- v(发送) | 赋值 |
&&/|| 短路求值。+ 加数字、拼接字符串(string + 非string 是
类型错误,用 str() 显式转换)。位运算作用于 int(& | ^ 亦
可用于 bool)。整数除法向零截断;整数 x / 0、x % 0 是运行时
错误。真值判定:false、0、0.0、""、nil、空数组与错误值
为假,其余为真。