typestar

Structs in Odin

A kayak struct built by field name and by position, described by a plain proc.

package main

import "core:fmt"

Kayak :: struct {
    model:     string,
    length_ft: int,
    rudder:    bool,
}

describe :: proc(k: Kayak) {
    fmt.printfln("%s: %d ft, rudder %t", k.model, k.length_ft, k.rudder)
}

main :: proc() {
    stock := Kayak{model = "Petrel", length_ft = 17}
    custom := Kayak{"Auk", 14, true} // positional works too
    describe(stock)
    describe(custom)
}

How it works

  1. The struct lists three typed fields; alignment is the compiler's problem.
  2. Kayak{model = "Petrel", length_ft = 17} names fields; positional works too.
  3. Odin has no methods — describe(stock) is just a proc taking the struct.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
364
Tokens
99
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 85 seconds.

Type this snippet

Step 1 of 3 in Structs, enums & unions, step 13 of 27 in Language basics.

← Previous Next →

Structs in other languages