强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

Lua 从入门到精通

Lua 从入门到精通 / Lua: From Beginner to Expert

一套由浅入深的 Lua 完整教程,每个场景纵向穿透。 A comprehensive Lua tutorial — each topic drilled from surface to core.


目录 / Table of Contents

# 章节 / Chapter 关键词 / Keywords
1 初识 Lua / Introduction What, Why, Where, REPL
2 语法基础 / Syntax Basics Variables, Types, Comments, Operators
3 数据类型 / Data Types nil, boolean, number, string, IEEE 754
4 控制流程 / Control Flow if, while, for, repeat, goto
5 函数 / Functions Def, Params, Variadic, Closure, Tail Call
6 表 / Tables Array, Dict, Hash, Weak Table, Rehash
7 字符串与模式匹配 / Strings & Patterns Pattern, Capture, Gsub, Interning
8 元表与元方法 / Metatables & Metamethods __index, __newindex, __add, Proxy
9 面向对象 / OOP Self, Colon, Inheritance, Encapsulation
10 模块与包 / Modules & Packages Require, Package.path, Sandbox
11 协程 / Coroutines Create, Resume, Yield, Producer-Consumer
12 错误处理 / Error Handling Error, Pcall, Xpcall, Traceback
13 文件与 I/O / File & System I/O io.open, os.execute, io.popen
14 垃圾回收 / Garbage Collection GC, Incremental, Generational, Weak Table
15 调试库 / Debug Library getinfo, getlocal, Hook, Traceback
16 性能优化 / Performance Local vs Global, LuaJIT, FFI, JIT
17 C API / Lua C API Embedding, Stack, Userdata, Registry
18 实战案例 / Real-World Projects Scripting, OpenResty, Redis, Game Engine

阅读建议 / How to Read

  • 初学者 / Beginners: 从第 1 章开始,按顺序阅读 / Start from Chapter 1, read in order.
  • 有经验者 / Experienced: 直接跳到感兴趣的章节 / Jump to the chapter you need.
  • 查阅用途 / Reference: 每章独立成文,可按需查阅 / Each chapter is self-contained.

每章结构 / Chapter Structure:

  • 🟢 基础 — 最简单的用法 / Basic — simplest usage
  • 🟡 进阶 — 生产环境常见用法 / Intermediate — production-ready
  • 🔴 高级 — 深入原理与极限用法 / Advanced — internals & edge cases

Lua 在技术栈中的位置 / Where Lua Fits

┌──────────────────────────────────────────────────────────────┐
│                        Lua 生态 / Lua Ecosystem              │
├──────────────┬──────────────┬──────────────┬─────────────────┤
│  游戏脚本    │   Web 服务器  │   数据库      │   嵌入式设备    │
│  Game Script │  Web Server  │  Database    │   Embedded      │
├──────────────┼──────────────┼──────────────┼─────────────────┤
│  World of    │  OpenResty   │  Redis       │   LÖVE 2D       │
│  Warcraft    │  (Nginx+Lua) │  (Eval)      │                 │
│              │              │              │                 │
│  Roblox      │  Kong        │  Tarantool   │   eLua          │
│  (Luau)      │  (API GW)    │              │                 │
│              │              │              │                 │
│  Dota 2      │  Hapis       │  RethinkDB   │   NodeMCU       │
│  Modding     │              │              │   (IoT)         │
└──────────────┴──────────────┴──────────────┴─────────────────┘

为什么学 Lua? / Why Learn Lua?

优势 / Advantage 说明 / Description
极简 / Minimalist 整个语言只有 21 个关键字 / Only 21 keywords
高性能 / Fast LuaJIT 接近 C 的性能 / LuaJIT speed near C
嵌入式 / Embeddable C API 设计优雅,易于集成 / Elegant C API
便携 / Portable 纯 ANSI C 实现,到处可跑 / Pure ANSI C, runs everywhere
一等公民函数 / First-class Functions 函数式编程能力 / Functional programming
协程 / Coroutines 非抢占式并发 / Non-preemptive concurrency