Embedding Lua Scripts in Go with Shopify/go-lua

Elliot Forbes Elliot Forbes ⏰ 1 Minutes 📅 Dec 17, 2022

👋 Welcome Gophers! In this tutorial, we are going to be taking a look at how we can embed lua scripts in our Go applications.

Getting Started

You can use the Shopify/go-lua package to embed Lua in Go. Here’s an example of how to do it:

package main

import (
	"fmt"

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


func main() {
	// Create a new Lua state
	l := lua.NewState()
	// Load the standard libraries
	lua.OpenLibraries(l)
	
	// Load and run a Lua script
	if err := lua.DoFile(l, "script.lua"); err != nil {
		fmt.Println(err)
		return
	}
}

This example loads and runs a Lua script called “script.lua”. You can also execute Lua code directly by using the DoString function:

Conclusion

Hopefully this helps!