-
序
- 入门篇
- Lua 入门
-
Nginx
-
Nginx 新手起步
-
location 匹配规则
-
静态文件服务
-
日志
-
反向代理
-
负载均衡
-
陷阱和常见错误
- 环境搭建
-
Hello World
-
与其他 location 配合
-
获取 uri 参数
-
获取请求 body
-
输出响应体
-
日志输出
-
简单API Server框架
-
使用 Nginx 内置绑定变量
-
子查询
-
不同阶段共享变量
-
防止 SQL 注入
-
如何发起新 HTTP 请求
-
访问有授权验证的 Redis
-
select+set_keepalive 组合操作引起的数据读写错误
-
redis 接口的二次封装(简化建连、拆连等细节)
-
redis 接口的二次封装(发布订阅)
-
pipeline 压缩请求数量
-
script 压缩复杂请求
-
动态生成的 lua-resty-redis 模块方法
-
Nginx 新手起步
- LuaCjsonLibrary
- PostgresNginxModule
- LuaNginxModule
- LuaRestyDNSLibrary
- LuaRestyLock
- 测试
- Web 服务
- 火焰图
break,return 和 goto
break
语句 break
用来终止 while
、repeat
和 for
三种循环的执行,并跳出当前循环体,
继续执行当前循环之后的语句。下面举一个 while
循环中的 break
的例子来说明:
-- 计算最小的x,使从1到x的所有数相加和大于100
sum = 0
i = 1
while true do
sum = sum + i
if sum > 100 then
break
end
i = i + 1
end
print("The result is " .. i) -->output:The result is 14
copy
在实际应用中,break
经常用于嵌套循环中。
return
return
主要用于从函数中返回结果,或者用于简单的结束一个函数的执行。
关于函数返回值的细节可以参考 函数的返回值 章节。return
只能写在语句块的最后,一旦执行了 return
语句,该语句之后的所有语句都不会再执行。若要写在函数中间,则只能写在一个显式的语句块内,参见示例代码:
local function add(x, y)
return x + y
--print("add: I will return the result " .. (x + y))
--因为前面有个return,若不注释该语句,则会报错
end
local function is_positive(x)
if x > 0 then
return x .. " is positive"
else
return x .. " is non-positive"
end
--由于return只出现在前面显式的语句块,所以此语句不注释也不会报错
--,但是不会被执行,此处不会产生输出
print("function end!")
end
local sum = add(10, 20)
print("The sum is " .. sum) -->output:The sum is 30
local answer = is_positive(-10)
print(answer) -->output:-10 is non-positive
copy
有时候,为了调试方便,我们可以想在某个函数的中间提前 return
,以进行控制流的短路。此时我们可以将 return
放在一个 do ... end
代码块中,例如:
local function foo()
print("before")
do return end
print("after") -- 这一行语句永远不会执行到
end
copy
goto
LuaJIT 一开始对标的是 Lua 5.1,但渐渐地也开始加入部分 Lua 5.2 甚至 Lua 5.3 的有用特性。
goto
就是其中一个不得不提的例子。
有了 goto
,我们可以实现 continue
的功能:
for i=1, 3 do
if i <= 2 then
print(i, "yes continue")
goto continue
end
print(i, " no continue")
::continue::
print([[i'm end]])
end
copy
输出结果:
$ luajit test.lua
1 yes continue
i'm end
2 yes continue
i'm end
3 no continue
i'm end
copy
在 GotoStatement 这个页面上,你能看到更多用 goto
玩转控制流的脑洞。
goto
的另外一项用途,就是简化错误处理的流程。有些时候你会发现,直接 goto 到函数末尾统一的错误处理过程,是更为清晰的写法。
local function process(input)
print("the input is", input)
if input < 2 then
goto failed
end
-- 更多处理流程和 goto err
print("processing...")
do return end
::failed::
print("handle error with input", input)
end
process(1)
process(3)
copy
PS :推荐大家多关注:http://luajit.org/extensions.html#lua52 , 说不定你就发现了新大陆。