歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Lua基礎入門 初識Lua

Lua基礎入門 初識Lua

日期:2017/3/1 10:05:43   编辑:Linux編程

跟學習其他的編程語言一樣,我也從hello world開始

用vim新建一個文件,hello.lua,在文件中輸入一行 print("hello world"),用命令:x保存退出

在shell界面,輸入lua hello.lua,hello world就會在屏幕中出現了

下面我們嘗試來定義個函數

新建一個文件 func_test.lua

內容如下:

-- define a factorial funcition
function fact (n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end

print("enter a number:");
a=io.read("*number") -- read a number
print(fact(a))

不要問我這段代碼什麼意思,程序員應該都看得出來的,運行一下

看完上面兩個簡單的示例,是不是感覺lua語言很方便啊。起碼我這個寫C的coder是感覺它好簡潔的。

Copyright © Linux教程網 All Rights Reserved