Go · Level 2 Types

Constants and iota

Model order statuses as a Go enum with const and iota, give them a String method, and keep the zero value for Unknown so an unset status never reads as a real one.

  • a named type: type Status int
  • iota counts up from 0 in a const block
  • the zero value should mean unknown

Go has no enum keyword: declare type Status int, then constants with iota, which counts 0, 1, 2… in a const block. Trap: a variable you never set holds 0, so make 0 mean Unknown, not a real status like Pending.

type Size int

const (
    SizeUnknown Size = iota  // 0
    SizeSmall                // 1
    SizeLarge                // 2
)

Declare Unknown, Pending, Paid and Shipped of type Status with iota, and write (s Status) String() string returning "unknown", "pending", "paid" or "shipped" ("unknown" for any other value). Example: Paid.String() → "paid".

solution.go
Test Suites Pending
Output