Control Flow in Golo
Getting Started with Golo
🚧 This is a work in progess
Golo provides familiar control flow constructs: if/else for conditionals, C-style for loops, foreach for iterating over collections, while loops, and match expressions for pattern-based branching (similar to switch but with condition guards via when/then).
control-flow.golo:
module getting.started.ControlFlow
function main = |args| {
# If-else
let x = 8
if x > 10 {
println("big")
} else if x > 5 {
println("medium")
} else {
println("small")
}
let age = 20
if age >= 18 {
println("You are an adult")
} else {
println("You are a minor")
}
# For loop
for (var i = 1, i <= 5, i = i + 1) {
println("i =", i)
}
# Foreach
foreach item in list["one", "two", "three"] {
println(item)
}
# While
var counter = 0
while counter < 5 {
println(counter)
counter = counter + 1
}
# Match
let classify = |n| {
return match {
when n < 0 then "negative"
when n == 0 then "zero"
when n > 0 then "positive"
otherwise "unknown"
}
}
println("Classify -5:", classify(-5))
println("Classify 0:", classify(0))
println("Classify 10:", classify(10))
}
run
golo control-flow.golo
Output:
medium
You are an adult
i = 1
i = 2
i = 3
i = 4
i = 5
one
two
three
0
1
2
3
4
Classify -5: negative
Classify 0: zero
Classify 10: positive