Control Structure
-
if - else
if user != nil{ } else { } if message := "hello"; user != nil{ // message is only available inside the if and else } else { }
-
switch(reloaded!)
This is a simple switch operation. No break is needed; you can fallthrough to the next case, thoughswitch day{ case "Monday": fmt.Println("It is Monday") case "Saturday": fallthrough case "Sunday": fmt.Println("This is sunday") default: fmt.Println("It is another working day") }
Switch with condition
It can replace large ifsswitch { case user == nil: case user.active == false: case user.deleted == true: default: }
-
for
for i:=0; i<len(collection); i++{ } // For range, similar to "for in" in JS for index:=range collection { } // For range, similar to "foreach" for key, value := range map { }
We can emulate a while by using a boolean expression
endOfGame := false for endOfGame { // process Game Loop } for count < 10 { count += 1; } for { // Infinite loop }
-
There is no while or do-while
-
No parenthesis are needed for boolean conditions or values
-
Only one type of equality operators ==
-
Other operators != < > <= >=