Control structures
In Go lang, loops are simplified. There is no for
, foreach
, while
, dowhile
etc. There is just a for loop.
for
loop
A minimum and an infinite for loop. Ended by break
statement:
for {
//statements
break
}
Typical for loop pattern - follows C pattern:
for i=0; i<count; i++{
//statements
//executes until i reaches count
}
Conditional for loop (like a while loop):
for <condition>{
// executes until condition remains true
}
Foreach style loop:
for index,element := range <slice/array> {
//statements
// range is an enumerator keyword
break
}
if
statements
Comparison operators commonly used with if
: <, >, <=, >=, ==, !=
.
Logical operators: &&, ||, !
for AND, OR, NOT evaluations.
if <condition> {
//statements, executes if eval to True
}
//single condition
if remainingTickets < 0 {
//
}
// complex condition
if (remainingTickets < 0) && (len(bookings) > 10) {
// two evals using and operator
}
if-else
, if-else-if
statements
if <condition> {
}
else {
}
if <condition> {
}
else if{
}
else {
}
Switch statements
city := "London"
switch city {
case "New York":
//statements
case "Mumbai":
//statements
case "London":
//statements
case "Los Angeles", "Pasadena": //common outcome
// statments for LA and Pas
default:
//statements
}