typestar

Maps en Odin

Un registro portuario en un map: make, indexar, el idioma del ok e iteración.

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)
    }
}

Cómo funciona

  1. make(map[string]int) reserva del allocator del context; delete limpia.
  2. Una clave ausente da el valor cero; spot, ok := te dice que faltaba.
  3. for ship, berth in berths recorre clave y valor juntos.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
19
Caracteres a escribir
428
Tokens
90
Ritmo de tres estrellas
70 tpm

Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 77 segundos.

Escribe este fragmento

Paso 3 de 3 en Colecciones; paso 12 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →

Maps en otros lenguajes