lua

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2025 License: MIT Imports: 2 Imported by: 0

README

lua-go

lua-go is a Go package that provides a simple and idiomatic wrapper for embedding and interacting with the Lua programming language. It allows Go applications to execute Lua code, manipulate Lua global variables, and evaluate Lua expressions, bridging the gap between Go and Lua.

Features

  • Execute Lua Code: Run arbitrary Lua code strings directly from Go.
  • Global Variable Access: Get global variables from the Lua state, supporting various Lua types (string, number, boolean, nil).
  • Evaluate Lua Expressions: Evaluate Lua code and retrieve multiple return values.

Installation

To install lua-go, use go get:

go get github.com/meinside/lua-go

Usage

Here's a basic example of how to use lua-go in your Go application:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/meinside/lua-go"
)

func main() {
	// Create a new Lua state
	s := lua.NewState()
	defer s.Close() // Ensure the Lua state is closed when done

	ctx := context.TODO()

	// Execute some Lua code
	err := s.Execute(ctx, `
		message = "Hello from Lua!"
		x = 10
		y = 20
		sum = x + y
		function multiply(a, b)
			return a * b
		end
	`)
	if err != nil {
		log.Fatalf("Error executing Lua code: %v", err)
	}

	// Get global variables
	fmt.Printf("message: %v\n", s.GetGlobal(ctx, "message")) // Output: Hello from Lua!
	fmt.Printf("sum: %v\n", s.GetGlobal(ctx, "sum"))         // Output: 30

	// Evaluate a Lua expression (calling a function)
	results, err := s.Evaluate(ctx, `return multiply(5, 6)`)
	if err != nil {
		log.Fatalf("Error evaluating Lua expression: %v", err)
	}
	if len(results) > 0 {
		fmt.Printf("multiply(5, 6): %v\n", results[0]) // Output: 30
	}

	// Evaluate an expression with multiple return values
	results, err = s.Evaluate(ctx, `return "apple", 123, true`)
	if err != nil {
		log.Fatalf("Error evaluating Lua expression: %v", err)
	}
	fmt.Printf("Multiple results: %v\n", results) // Output: [apple 123 true]
}

Todos

  • Support return types: 'function', 'userdata', and 'thread'.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Overview

Package lua provides a wrapper for running Lua codes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type State

type State struct {
	// contains filtered or unexported fields
}

State wraps the low-level Lua state.

func NewState

func NewState() *State

NewState creates a new Lua state.

func (*State) Close

func (s *State) Close()

Close closes the Lua state.

func (*State) Evaluate

func (s *State) Evaluate(ctx context.Context, code string) ([]any, error)

Evaluate evaluates a string of Lua code and returns its results.

func (*State) Execute

func (s *State) Execute(ctx context.Context, code string) error

Execute executes a string of Lua code.

func (*State) GetGlobal

func (s *State) GetGlobal(ctx context.Context, name string) any

GetGlobal gets a global variable from the Lua state.

Directories

Path Synopsis
Package luasrc provides a wrapper for the low-level Lua C API.
Package luasrc provides a wrapper for the low-level Lua C API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL