typestar

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

  1. make(map[string]int) allocates from the context allocator; delete cleans up.
  2. A missing key yields the zero value; spot, ok := tells you it was missing.
  3. for ship, berth in berths walks key and value together.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Collections, step 12 of 27 in Language basics.

← Previous Next →

Maps in other languages