Go Conditionals and Loops
Control flow with if statements and loops
If-Else Statements
if condition { # check condition
// code
} else if condition2 { # additional check
// code
} else { # default case
// code
}
If with Initialization
if x := getValue(); x > 10 { # declare and check
fmt.Println(x) # x available in if block
}
Switch Statement
switch value { # evaluate value
case 1: # if value is 1
// code (no break needed)
case 2, 3: # multiple values
// code
default: # if no match
// code
}
For Loop
for i := 0; i < 10; i++ { # standard for loop
fmt.Println(i)
}
for condition { # while loop equivalent
// code
}
for { # infinite loop
// code
}
Range Loop
arr := []int{1, 2, 3}
for index, value := range arr { # iterate with index and value
fmt.Println(index, value)
}
for _, value := range arr { # ignore index
fmt.Println(value)
}
Loop Control
break # exit loop
continue # skip to next iteration