Skip to content

List operations

Example: matrix/2d array operations

// Store 2D array (list of lists)
matrix := [][]int{
[]int{1, 2, 3},
[]int{4, 5, 6},
[]int{7, 8, 9},
}
key, _ := as.NewKey("app", "data", "matrix1")
_ = client.Put(nil, key, as.BinMap{"matrix": matrix})
// Append to second row (index 1)
ctx := []*as.CDTContext{
as.CtxListIndex(1),
}
_, err = client.Operate(nil, key,
as.ListAppendOp("matrix", 10),
)
// Get value from row 1, column 2
ctx = []*as.CDTContext{
as.CtxListIndex(1),
}
record, err := client.Operate(nil, key,
as.ListGetOp("matrix", 2, ctx...),
)
value := record.Bins["matrix"]
fmt.Println("Value at [1][2]:", value) // Output: Value at [1][2]: 6

Example: user with nested order history

// Store user with order history (list of order maps)
user := map[string]any{
"name": "John Doe",
"orders": []any{
map[string]any{"id": "ord1", "total": 100},
map[string]any{"id": "ord2", "total": 200},
},
}
key, _ := as.NewKey("store", "users", "user123")
_, err := client.Put(nil, key, as.BinMap{"user": user})
// Add new order to history
newOrder := map[string]any{
"id": "ord3",
"total": 150,
}
ctx := []*as.CDTContext{
as.CtxMapKey(as.StringValue("orders")),
}
lp := as.NewListPolicy(
as.ListOrderUnordered,
as.ListWriteFlagsAddUnique,
)
_, err := client.Operate(nil, key,
as.ListAppendWithPolicyContextOp(lp, "user", ctx, newOrder),
)
record, _ := client.Operate(nil, key,
as.ListGetByIndexRangeOp(
"user",
0,
as.ListReturnTypeValue,
ctx...,
),
)
orders := record.Bins["user"].([]any)
lastOrder := orders[2].(map[string]any)
fmt.Println(lastOrder["id"]) //ord3
fmt.Println(orders) // [map[id:ord1 total:100] map[id:ord2 total:200] map[id:ord3 total:150]]

Example: getting nested values directly with chained context

When you have a map containing a list of maps, you can use chained context to get a specific value directly without retrieving the entire nested structure.

// Store order: map containing list of items (each item is a map)
order := map[string]any{
"orderId": "ord123",
"customerId": "cust456",
"items": []any{
map[string]any{"productId": "prod1", "quantity": 2, "price": 10.0},
map[string]any{"productId": "prod2", "quantity": 1, "price": 20.0},
},
"total": 40.0,
}
key, _ := as.NewKey("store", "orders", "ord123")
_, err := client.Put(nil, key, as.BinMap{"order": order})
// Get productId from first item directly using chained context
// Context chain: items (map key) -> [0] (list index) -> productId (map key)
ctx := []*as.CDTContext{
as.CtxMapKey(as.StringValue("items")), // Step 1: Navigate to "items" list
as.CtxListIndex(0), // Step 2: Navigate to first item (index 0)
}
record, err := client.Operate(nil, key,
as.MapGetByKeyOp("order", "productId", as.MapReturnType.VALUE, ctx...),
)
productId := record.Bins["order"].(string)
fmt.Println("First product ID:", productId)
// Get quantity from second item
ctx = []*as.CDTContext{
as.CtxMapKey(as.StringValue("items")),
as.CtxListIndex(1), // Second item (index 1)
}
record, err = client.Operate(nil, key,
as.MapGetByKeyOp("order", "quantity", as.MapReturnType.VALUE, ctx...),
)
quantity := record.Bins["order"].(int)
fmt.Println("Second item quantity:", quantity)
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?