Directories in Lua
Here’s the translation of the Go code to Lua, along with explanations in Markdown format suitable for Hugo:
Our first example demonstrates how to work with directories in Lua. Here’s the full source code:
local lfs = require("lfs")
-- Helper function to check for errors
local function check(err)
if err then
error(err)
end
end
-- Helper function to create an empty file
local function createEmptyFile(name)
local file = io.open(name, "w")
file:close()
end
-- Create a new sub-directory in the current working directory
check(lfs.mkdir("subdir"))
-- When creating temporary directories, it's good practice to remove them afterwards
local function cleanup()
os.execute("rm -rf subdir")
end
createEmptyFile("subdir/file1")
-- We can create a hierarchy of directories, including parents
check(lfs.mkdir("subdir/parent/child"))
createEmptyFile("subdir/parent/file2")
createEmptyFile("subdir/parent/file3")
createEmptyFile("subdir/parent/child/file4")
-- List directory contents
print("Listing subdir/parent")
for file in lfs.dir("subdir/parent") do
local attr = lfs.attributes("subdir/parent/" .. file)
print(" ", file, attr.mode == "directory")
end
-- Change the current working directory
check(lfs.chdir("subdir/parent/child"))
-- Now we'll see the contents of subdir/parent/child when listing the current directory
print("Listing subdir/parent/child")
for file in lfs.dir(".") do
local attr = lfs.attributes(file)
print(" ", file, attr.mode == "directory")
end
-- Change back to where we started
check(lfs.chdir("../../.."))
-- We can also visit a directory recursively, including all its sub-directories
print("Visiting subdir")
local function visit(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local f = path..'/'..file
print(" ", f, lfs.attributes(f).mode == "directory")
if lfs.attributes(f).mode == "directory" then
visit(f)
end
end
end
end
visit("subdir")
-- Clean up
cleanup()
To run the program, save it as directories.lua
and use lua
:
$ lua directories.lua
Listing subdir/parent
child true
file2 false
file3 false
Listing subdir/parent/child
file4 false
Visiting subdir
subdir/file1 false
subdir/parent true
subdir/parent/child true
subdir/parent/child/file4 false
subdir/parent/file2 false
subdir/parent/file3 false
This Lua script demonstrates various operations with directories:
- We use the
lfs
(LuaFileSystem) module for directory operations. - We create directories using
lfs.mkdir()
. - We list directory contents using
lfs.dir()
. - We change the current working directory with
lfs.chdir()
. - We recursively visit a directory structure using a custom
visit()
function.
Note that Lua doesn’t have built-in functions for all directory operations, so we use the lfs
module and some OS commands. The error handling is simplified, and some operations (like creating empty files) are done differently due to language differences.
The cleanup function simulates the defer
concept from Go, ensuring that temporary directories are removed at the end of the script.