Go Integration
Complete guide for embedding Scriptling in Go applications.
Installation
go get github.com/paularlott/scriptlingQuick Start
package main
import (
"fmt"
"github.com/paularlott/scriptling"
"github.com/paularlott/scriptling/stdlib"
)
func main() {
// Create interpreter
p := scriptling.New()
// Register standard libraries
stdlib.RegisterAll(p)
// Execute Scriptling code
result, err := p.Eval(`x = 5 + 3`)
if err != nil {
fmt.Println("Error:", err)
}
}Topics
- Basics - Creating interpreters, variable exchange, calling functions
- Go Functions - Registering Go functions callable from Scriptling
- Go Libraries - Creating Go libraries with functions and constants
- Go Classes - Creating Go classes for object-oriented integration
- Builder API - Type-safe builder pattern for performance
Two Integration Approaches
Native API
Direct control with maximum performance:
p.RegisterFunc("add", func(ctx context.Context, kwargs object.Kwargs, args ...object.Object) object.Object {
a, _ := args[0].AsInt()
b, _ := args[1].AsInt()
return &object.Integer{Value: a + b}
})Builder API
Type-safe, cleaner syntax with automatic conversion:
fb := object.NewFunctionBuilder()
fb.FunctionWithHelp(func(a, b int) int {
return a + b
}, "add(a, b) - Add two numbers")
p.RegisterFunc("add", fb.Build())Performance Tips
- Reuse Interpreters - Create once, use multiple times
- Load Only Needed Libraries - Don’t load JSON/HTTP if not needed
- Batch Operations - Execute larger scripts rather than many small ones
- Pre-register Functions - Register all Go functions before execution
- Use Native API for Hot Paths - Avoid reflection overhead in tight loops
// Good: Reuse interpreter
p := scriptling.New()
for _, script := range scripts {
p.Eval(script)
}