Data Modeling Patterns
Iota Status
Iota constants give small state codes clear names without storing magic numbers in the code.
iota constants
Keep the integer representation compact while using named constants at each decision point.
Iota Status
iota_status.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
const (
StatusDraft = iota
StatusReview
StatusPublished
)
func main() {
var status = StatusReview
label := "unknown"
if status == StatusDraft {
label = "draft"
} else if status == StatusReview {
label = "review"
} else if status == StatusPublished {
label = "published"
}
ready := status == StatusPublished
fmt.Println("status=", status)
fmt.Println("label=", label)
fmt.Println("ready=", ready)
}
package main
import "fmt"
const (
StatusDraft = iota
StatusReview
StatusPublished
)
func main() {
var status = StatusDraft
label := "unknown"
if status == StatusDraft {
label = "draft"
} else if status == StatusReview {
label = "review"
} else if status == StatusPublished {
label = "published"
}
ready := status == StatusPublished
fmt.Println("status=", status)
fmt.Println("label=", label)
fmt.Println("ready=", ready)
}
package main
import "fmt"
const (
StatusDraft = iota
StatusReview
StatusPublished
)
func main() {
var status = StatusPublished
label := "unknown"
if status == StatusDraft {
label = "draft"
} else if status == StatusReview {
label = "review"
} else if status == StatusPublished {
label = "published"
}
ready := status == StatusPublished
fmt.Println("status=", status)
fmt.Println("label=", label)
fmt.Println("ready=", ready)
}
status ← 1, StatusReview ← 1, label ← "unknown"
11func main() {12 var status→ 1 = StatusReview→ 1 //@status=StatusDraft, StatusPublished13 label→ "unknown" := "unknown"14 if status == StatusDraft {label ← "review"
15 label = "draft"16} else if status1 == StatusReview1 {17 label→ "review" = "review"18} else if status == StatusPublished {ready ← false
20 }21 ready→ false := status1 == StatusPublished22223 fmt.Println("status=", status1)24 fmt.Println("label=", label"review")25 fmt.Println("ready=", readyfalse)26}outputstatus= 1 label= review ready= false
status ← 0, StatusDraft ← 0, label ← "unknown"
11func main() {12 var status→ 0 = StatusDraft→ 013 label→ "unknown" := "unknown"14 if status == StatusDraft {label ← "draft"
13label := "unknown"14if status0 == StatusDraft0 {15 label→ "draft" = "draft"16} else if status == StatusReview {ready ← false
20 }21 ready→ false := status0 == StatusPublished22223 fmt.Println("status=", status0)24 fmt.Println("label=", label"draft")25 fmt.Println("ready=", readyfalse)26}outputstatus= 0 label= draft ready= false
status ← 2, StatusPublished ← 2, label ← "unknown"
11func main() {12 var status→ 2 = StatusPublished→ 213 label→ "unknown" := "unknown"14 if status == StatusDraft {label ← "published"
17 label = "review"18} else if status2 == StatusPublished2 {19 label→ "published" = "published"20}ready ← true
20 }21 ready→ true := status2 == StatusPublished22223 fmt.Println("status=", status2)24 fmt.Println("label=", label"published")25 fmt.Println("ready=", readytrue)26}outputstatus= 2 label= published ready= true