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.

If/Else

Here is a example of a 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 alot of different tests, a more concise and easier logical test operator is the Switch statement, which will be described next.

Switch

Here is an example of a typical switch statement:

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

Besides the “case” blocks, there are 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 runing 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:

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why we use new technologies to build your software.

Using culling layer masks for Virtual Cameras in Unity 2021

How to configure a Hyperledger Fabric node for production

How to get started with Azure Functions

Future Plans for Glorious Rigby & All Things Lambda.

Entropy of abstract syntax trees

An Open-Source Tool to Extract Tables from PDFs into CSVs

That time I contributed to Scala Exercises website

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

Nikolaj Johannes Skole Jensen

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

More from Medium

Message Broker — A Quick Introduction to RabbitMQ

What is API ….!! simple explanation

BST Sequences

Commands shortcuts for Command Line I