go-lua-uuid
A GopherLua module for UUID generation and validation in Lua scripts.
Installation
go get github.com/kerimovok/go-lua-uuid
Usage
In Go
import (
lua "github.com/yuin/gopher-lua"
uuid "github.com/kerimovok/go-lua-uuid"
)
L := lua.NewState()
defer L.Close()
// Preload the uuid module
L.PreloadModule("uuid", uuid.Loader)
// Now Lua scripts can use require("uuid")
L.DoString(`
local uuid = require("uuid")
local id = uuid.new()
print(id)
`)
In Lua
local uuid = require("uuid")
-- Generate a new UUID v4
local id = uuid.new()
print("Generated UUID:", id)
-- Alternative: use v4() function
local id2 = uuid.v4()
print("Another UUID:", id2)
-- Validate a UUID
local isValid = uuid.is_valid("550e8400-e29b-41d4-a716-446655440000")
if isValid then
print("Valid UUID")
else
print("Invalid UUID")
end
-- Parse and validate a UUID string
local parsed, err = uuid.parse("550e8400-e29b-41d4-a716-446655440000")
if err then
error("Invalid UUID: " .. err)
end
print("Parsed UUID:", parsed)
Functions
uuid.new()
Generates a new UUID v4.
- Returns:
string: UUID string in standard format (e.g., "550e8400-e29b-41d4-a716-446655440000")
uuid.v4()
Generates a new UUID v4 (alias for new()).
uuid.parse(str)
Parses and validates a UUID string.
- Parameters:
str (string): UUID string to parse
- Returns:
string: Parsed UUID string (or nil on error)
string (error): Error message if parsing fails
uuid.is_valid(str)
Checks if a string is a valid UUID.
- Parameters:
str (string): String to validate
- Returns:
boolean: true if valid UUID, false otherwise
Notes
- All UUIDs generated are version 4 (random UUIDs)
- UUID format:
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B
- The module uses Google's UUID library for generation and validation