px普贤教程PuXian
普贤教程 › 进阶教程

标准库概览

标准库随语言分发,共 27 个模块,全部是 .px 源码(可读、可改、可抄)。用 import std.名称 引入。

按用途分类

文本与数据

模块关键函数用途
std.stringsindex_of last_index_of count_of子串定位(语言内置里没有这些原语)
std.collectionsunique flatten zip_lists chunk group_by sort_by列表处理的那几个常用动作
std.jsonxjson_pretty json_pretty_indent把紧凑 JSON 变成带缩进的可读格式
std.go_json / go_json_indent / go_json_scan数字加宽、缩进、扫描与 Go 的 encoding/json 行为对齐
std.go_stringsgo_index go_slice按字节下标操作(与 Go 语义一致)
std.htmlHTML 节点栈、转义拼 HTML、做简单的模板渲染
std.pxmlXML 解析读配置文件、解析 RSS
std.yaml / yamlx / yaml_lex / yaml_linesyaml_parse 等YAML 读写(Ma 的配置文件就是 YAML)
std.semversv_parse sv_cmp sv_satisfies版本号比较、范围匹配

网络

模块关键函数用途
std.urlurl_query_escape url_parseURL 编解码、查询串解析
std.smtp构造邮件拼 multipart 邮件体(配 TLS 发送)
std.multipartmp_encode mp_boundary文件上传的表单编码
std.cookiejarcj_new cj_update cj_headerHTTP Cookie 管理
std.webroutewr_parse_file「文件即路由」的命名解析

系统、文件与图像

模块关键函数用途
std.pathpath_join拼路径,避免手写斜杠
std.iowrite_all write_line分块写文件描述符
std.edgeGPIO / I2C / 串口 / PWM边缘设备(树莓派、工控板)直接操作硬件
std.pngPNG 编解码生成或读取 PNG
std.gfx画布、线、文字画验证码、图表(注意性能,见性能一章)
std.time_gogo_format go_rfc3339 go_dateGo 风格的时间格式化
std.lunar农历转换农历 / 节气

用法示例

px
import std.collections
import std.jsonx
import std.path

var xs = [3, 1, 2, 1, 3]
print(str(unique(xs)))
print(str(chunk([1, 2, 3, 4, 5], 2)))
print(json_pretty({"a": [1, 2], "b": {"c": true}}))
print(path_join("/data/www", "px_dzty_club"))
text
[3, 1, 2]
[[1, 2], [3, 4], [5]]
{
  "a": [
    1,
    2
  ],
  "b": {
    "c": true
  }
}
/data/www/px_dzty_club

生态包(registry)

标准库之外还有 195 个包,覆盖数据库、加密、图像、二维码、日志、测试等。常用的几个:

包能力
mysql / pg / redis / mongodb数据库客户端
jwt / bcrypt / passhash / totp鉴权与密码学
captcha / qrcode / barcode验证码与码制
smtp / pop3 / imap邮件收发
log / metrics / retry / sched可观测性与调度
actor / workerpool / concurrent_map / semaphore并发封装
validator / idcard / pinyin / cnnum国内业务常用校验与转换
testkit / quickcheck / mock测试
uuid / nanoid / ulid / snowflakeID 生成
deploy / config / dotenv / tempfile工程配套

安装与使用见 包管理与 registry。

标准库是源码不确定某个函数怎么用,直接打开 /usr/share/puxian/stdlib/xxx.px 看实现——注释里通常写着动机、边界和与 Go 的对照。这比查文档快。