Skip to content
Examples
Working with Maps

Working with Maps

Maps are a container for key-value pairs. In Risor, map keys must always be strings, which provides equivalence between maps and JSON objects. Under the hood, Risor maps are made from map[string]interface{} in Go.

Declaring Maps

It is optional to surround keys in quotes when declaring a map literal:

{ name: "Alice", age: 30 }

Iterating Over a Map

m := {
    a: 1,
    b: 2,
    c: 3,
}
 
for k, v := range m {
    print("k:", k, "v:", v)
}
Output:
k: a v: 1
k: b v: 2
k: c v: 3

Indexing a Map

user := {
    name: "John",
    age:  30,
}
 
print(user["name"])
Output:
John

Delete an Item

user := {
    name: "John",
    age:  30,
}
 
delete(user, "age")
 
print(keys(user))
Output:
[name]