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
make(map[string]int)reserva del allocator del context;deletelimpia.- Una clave ausente da el valor cero;
spot, ok :=te dice que faltaba. for ship, berth in berthsrecorre clave y valor juntos.
Palabras clave y builtins usados aquí
deferdeleteforimportinintmakemappackageprocstring
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.
Paso 3 de 3 en Colecciones; paso 12 de 27 en Fundamentos del lenguaje.