Returns the last element of a string slice. Errors if the slice is empty.
| Field | Type | Description |
|---|---|---|
Input | *[]string | Slice to read from |
| Field | Type | Description |
|---|---|---|
Result | string | Last element |
Errors on empty slice. Use IfEmptySliceStringOp first if the slice could be empty.
Extracts the final step from a pipeline's ordered step list to determine the last action taken.
package main
import (
"context"
"encoding/json"
"log"
"os"
"time"
"github.com/akennis/sparsi-go/library"
_ "github.com/akennis/dagor/operator/builtin"
"github.com/panjf2000/ants/v2"
"github.com/akennis/dagor"
"github.com/akennis/dagor/graph"
)
func main() {
steps := []string{"validate", "enrich", "route", "notify"}
library.RegisterConst("steps_val", steps)
g, err := graph.NewBuilder("slicelast_demo").
Vertex("src").Op("steps_val").Output("Result", "wire_in").
Vertex("lst").Op("SliceLastOp").
Input("Input", "wire_in").
Output("Result", "last_step").
Build()
if err != nil {
log.Fatalf("build: %v", err)
}
pool, err := ants.NewPool(4)
if err != nil {
log.Fatalf("pool: %v", err)
}
defer pool.Release()
eng, err := dagor.NewEngine(g, pool)
if err != nil {
log.Fatalf("engine: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
if err := eng.Run(ctx); err != nil {
log.Fatalf("run: %v", err)
}
out := map[string]any{"steps": steps}
if raw, ok := eng.GetOutput("last_step"); ok {
out["last_step"] = *raw.(*string)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(out)
}