强曰为道

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

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 / IntroductionWhat, Why, Where, REPL
2语法基础 / Syntax BasicsVariables, Types, Comments, Operators
3数据类型 / Data Typesnil, boolean, number, string, IEEE 754
4控制流程 / Control Flowif, while, for, repeat, goto
5函数 / FunctionsDef, Params, Variadic, Closure, Tail Call
6表 / TablesArray, Dict, Hash, Weak Table, Rehash
7字符串与模式匹配 / Strings & PatternsPattern, Capture, Gsub, Interning
8元表与元方法 / Metatables & Metamethods__index, __newindex, __add, Proxy
9面向对象 / OOPSelf, Colon, Inheritance, Encapsulation
10模块与包 / Modules & PackagesRequire, Package.path, Sandbox
11协程 / CoroutinesCreate, Resume, Yield, Producer-Consumer
12错误处理 / Error HandlingError, Pcall, Xpcall, Traceback
13文件与 I/O / File & System I/Oio.open, os.execute, io.popen
14垃圾回收 / Garbage CollectionGC, Incremental, Generational, Weak Table
15调试库 / Debug Librarygetinfo, getlocal, Hook, Traceback
16性能优化 / PerformanceLocal vs Global, LuaJIT, FFI, JIT
17C API / Lua C APIEmbedding, Stack, Userdata, Registry
18实战案例 / Real-World ProjectsScripting, 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
高性能 / FastLuaJIT 接近 C 的性能 / LuaJIT speed near C
嵌入式 / EmbeddableC API 设计优雅,易于集成 / Elegant C API
便携 / Portable纯 ANSI C 实现,到处可跑 / Pure ANSI C, runs everywhere
一等公民函数 / First-class Functions函数式编程能力 / Functional programming
协程 / Coroutines非抢占式并发 / Non-preemptive concurrency