Maps in Odin
A harbor registry in a map: make, index, the ok idiom, and iteration.
package main
import "core:fmt"
main :: proc() {
// make allocates from the context allocator; delete cleans up.
berths := make(map[string]int)
defer delete(berths)
berths["Persephone"] = 9
berths["Skua"] = 4
// Indexing a missing key yields the zero value; the ok form tells.
spot, ok := berths["Dreadnought"]
fmt.println("unknown ship:", spot, ok)
for ship, berth in berths {
fmt.printfln("%s moors at %d", ship, berth)
}
}
How it works
make(map[string]int)allocates from the context allocator;deletecleans up.- A missing key yields the zero value;
spot, ok :=tells you it was missing. for ship, berth in berthswalks key and value together.
Keywords and builtins used here
deferdeleteforimportinintmakemappackageprocstring
The run, in numbers
- Lines
- 19
- Characters to type
- 428
- Tokens
- 90
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 77 seconds.
Step 3 of 3 in Collections, step 12 of 27 in Language basics.