Startup People! Here Is Your 90 Day Marketing Plan

First 90 days is always pivotal for any business. You would have experienced it probably, or your next client might say it to you, or your boss would have told you, or you might be asked in your next…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Control structures in Go

Control structures are useful in many ways. We use it to analyze variables and choose a direction to go in our program. Based on different criteria that is given as parameters we can make the program do different kind of things based on that.

The if statement looks like the following below.

Note: The given variables x and y are not declared, by doing that i keep this code abstract as possible while explaining it.

and if the condition is not fulfilled we could also use the else statement on top of the if statement, example given below.

if the first condition isn’t fulfilled we could also add the else if statement to check another condition before the else statement is executed, example given below.

Okey, I’ve now showed you a very simple usage of the if statement. We’ll now talk about how we can apply this type of control structure into our Go program and make it useful by also checking for errors when we call functions.

The example above is good by design, instead of writing it like the example below.

In Go we can redeclare an used variable within same the same scope, which basically means that we can do a legal duplicate of it and assign a new value to it. Example is given below, see how we make use of the err variable that is declared with a shorthand assignment in the beginning and then we use it again to reassign it to a new value that is returned by Stat() function.

Go only have one type of loop and that is the for loop. It can be done in three ways, the first one is given below. It takes an initialization, a condition and makes a variable change. for init; condition; post {}

The second way of doing a for loop is like the following. for condition {} which is like the C language while. As long as the condition is true it will run.

The third way of looping with for is the following example. for {}
It runs forever, basically it’s an infinite loop if you skip the condition statement. This type of for loop is very useful when you have to communicate and deal with multiple channels in Go.

By using the select statement inside of the for loop lets a goroutine wait on multiple communication operations. Example is given below. If you want to read more about goroutines and channels in go i recommend you these links below.

In Go we use the range clause that manage the loop for us, awesome right.
It looks like the following below.

Maybe we don’t need the value in this case, fine. We only remove the value from the for so it looks like this.

Switch tries to match the value you switch on the different cases that are given, cases are evaluated from top to bottom and if there is no expression for the switch it will switch on true. Example given below.

You can also use the switch to find out what the dynamic type of an interface variable is by doing following.

Add a comment

Related posts:

What is success after all?

My alarm went off at 4 am. It was quite humid in the room I was sleeping in on the small boat, despite the air-conditioner working to its maximum. I rolled over from the top bunker bed and quickly…

Why React Js?

React Js is a java script library which we use for creating user interfaces for web applications. It was created by Jordan Walke, who is an facebook employee. Then later it was developed by Facebook…

Shamed Into Exercising!

I just drove 8 hours home from vacation from (excuse me, “with”) my family, depending on your definition of the concept. I’ll think about that later. We didn’t all drive back together which is why I…