Go — If/Else, Switch, and For loop (basics)

Logic checks in GO can be performed in several different ways, two of these are: If/Else statements, and Switches. Both of these I will describe here.


Here is an example of an if/else block, which in this case always will result in the if logical test to return true, and therefore always will run the if-blocks code:

If you need more logical tests, you can also use the: “else if” statement:

If you need a lot of different tests, a more concise and easier logical test operator is the Switch statement, which will be described next.


Here is an example of a typical switch statement:

If one of the “case-blocks” has the same code, you can separate each case with commas to contain more checks:

Besides the “case” blocks, there is also a “default” block, which acts like the else block described earlier. The “default” block will run if none of the “cases” were logically true. Take the above screenshot, if the variable “i” was equal to 10, then a potential “default” block would run. That is because none of the “case” blocks caught this:

For loop

The for loop is useful for running some code block a number of times or based on items in an array, or some other thing you have to do a lot of times.

Here are some examples of different for loops in GO:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nikolaj Johannes Skole Jensen

Fullstack developer living in Denmark. Experience with both Web- and App development.