← Library SliceDeterministic

SliceAtOp

Returns the element at a fixed index from a string slice.

Inputs

FieldTypeDescription
Input*[]stringSlice to index into

Outputs

FieldTypeDescription
ResultstringElement at the configured index

Params

KeyTypeDefaultDescription
indexint0Zero-based index to retrieve

Typical Use Cases

  • Extracting a fixed-position result
  • Getting the top item after AIBestMatchOp
  • Accessing a known index in a parallel output

Complete Runnable Example

Retrieves the second tag (index 1) from a list of ticket tags assigned during triage.

main.go
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() {
	tags := []string{"urgent", "billing", "escalate"}
	library.RegisterConst("tags_val", tags)

	g, err := graph.NewBuilder("sliceat_demo").
		Vertex("src").Op("tags_val").Output("Result", "wire_in").
		Vertex("at").Op("SliceAtOp").
		Params(map[string]string{"index": "1"}).
		Input("Input", "wire_in").
		Output("Result", "tag").
		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{"tags": tags, "index": 1}
	if raw, ok := eng.GetOutput("tag"); ok {
		out["tag_at_index"] = *raw.(*string)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}