lua坑

1.nil的类型
nil有两种类型,一种是nil,另一种是userdata。两个nil不相等。userdata类型的nil是c引用中的null.
local json = require('cjson')
local n = json.decode("null")
print(nil == n)
print(type(n))
print(type(nil))

结果:
false
userdata
nil

在openresty中,userdata的nil可以用ngx.null进行比较。判空可使用下面代码
if ngx.null == n or nil == n then
	print("this is nil")
end


2.os.date日期格式化大坑
在windows下的openresty中,会有如下bug
ngx.say(os.date("%m月%d",1458035530))

返回:
03月%d

更大的坑
ngx.say(os.date("%m月%d日",1458035530))

程序将会终止执行,并且没有任何错误。建议日期格式化中不要包含非ascii字符。

猜你喜欢

转载自uzoice.iteye.com/blog/2283216
LUA