# 02 · table-driven-switch **演示**:函数表按字符串分派,替代 C/Java 的 `switch`;未知键返回 `nil, err`。 **运行时**:`lua54`、`luajit`。 **运行**: ```sh .tools/bin/lua54 scripts/run-example.lua examples/06-control-flow-and-truthiness/02-table-driven-switch .tools/bin/lua54 scripts/test-example.lua examples/06-control-flow-and-truthiness/02-table-driven-switch ``` **预期输出**(lua 5.4.9 / macOS arm64): ```text add=5 mul=6 unknown=true ``` **看点**:分派表可以动态增删,比 `if/elseif` 链更易扩展。 **注意**:表键是字符串时有哈希成本;热点路径可以缓存 handler。