Run Lua Modules
Lesson 1 of 48 in Coddy's Lua for Go Developers course.
You already know Go functions, slices, maps, structs and interfaces. This standalone course moves those skills to Lua 5.1. Run the supplied program first: main.lua loads app.lua with require("app"), and the module returns its public API table. Lua loads and executes chunks at runtime, then caches their module values. The locked driver reads input and prints results while you edit the other files.
Go and Lua both manage memory automatically, but their value and reference models differ. Replace slices and method sets with dense tables, explicit receiver conventions and captured state while deciding which mutable objects may be shared. These tasks use Lua 5.1 syntax, including math.floor and global unpack where needed. No earlier course is required.
Challenge
BeginnerRun the supplied program. No edits or input are required. It returns Lua is ready.
Edit app.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Try it yourself
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
All lessons in Lua for Go Developers
1First Lua Programs
Run Lua ModulesPrint and CommentNames and Local ScopeNumbers and ConversionStrings and Byte PositionsRecap: Format a Receipt4Functions and Closures
Functions and ArgumentsDefaults and Options TablesMultiple Results and VarargsCallbacks and Captured StateRecap: Pricing Functions2Values and Decisions
Absence and Table FieldsTruth and Default ValuesBranches and BoundsValues and Table IdentityRecap: Admission RulesPractice on your own: Online Lua compiler