This directory contains examples demonstrating the basic "one-time execution" pattern for script evaluation in go-polyscript.
The simple execution pattern is the most straightforward way to run scripts with go-polyscript. It creates an evaluator and immediately executes the script in a single operation, providing a clean, concise approach for one-off script executions.
This pattern is ideal for:
- Single-Use Scripts: Scripts that will only be executed once
- Configuration Processing: Loading and validating configuration files
- Simple Automation Tasks: One-time data transformations or validations
- Prototype Development: Quick implementations for testing concepts
- Isolated Operations: When script execution is independent of other operations
These examples follow a consistent pattern:
- Create the input data: Build a map with values the script needs
- Configure the data provider: Use
data.NewStaticProvider(input)
to make data available to the script - Configure the evaluator: Set up with options like
WithGlobals
to expose thectx
variable - Execute immediately: Call
evaluator.Eval(ctx)
to run the script - Process results: Convert the response to the expected type with
result.Interface()
In all examples, scripts access data using a ctx
global variable:
- Starlark:
name = ctx["name"]
- Risor:
name := ctx["name"]
- Extism: Input data is passed to the WASM module
Each example follows the same pattern but demonstrates it with a different script engine:
go run examples/simple/<engine>/main.go
Note: The Extism example requires a WebAssembly module. It uses the findWasmFile
function to locate the module in various directories.
- Multiple Instantiation Examples: Compile-once-run-many pattern
- Data Preparation Examples: Pattern for separating static configuration from dynamic runtime data