LEARN GOLANG| Part-1

In this blog, we shall be looking at following concepts :-

  • Anatomy of GoLang program.

Question:- Explain the anatomy of a GO program ?

Answer:- Let’s take a look at the small Go program and break it down line by line.

  • The first line is a comment. You can either use single-line comments, like this, or multi-line comments, which start with a forward slash and an asterisk and end with an asterisk and a forward slash, very much like C++ or Java.

Question :- Showcase any simple program written in GoLang, with a variable being declared into it ?

Question :- Explain the variable declaration statement within GoLang :-

Answer:- This is crucial statement and have been broken down below with explanation :-

  • var → The first word in here of var is short-form for variable. It informs go that we are about to create a new variable right after the word var.

Question :- Can you explain the difference between the “Statically Typed Language” and “Dynamically Typed Language” ?

Answer:- JavaScript, Ruby and Python are all examples of dynamically typed languages, whereas C++, java and Go are statically typed languages.

A dynamically typed language is one in which you and I, the developers, essentially do not care what values we are assigning to any given variable. So for example, I’m going to pop open my chrome console right here, which is going to allow me to just very quickly write out a little bit of JavaScript.

  • Here, we have assigned number variable being equals to numeral integer 123 and then immediately underneath that I’m going to say number equals “abcd” i.e. a string.

Question :- Is there any other way, that variable-declaration can be done ?

  • At line number-7 here, we are relying upon the go compiler, to just kind of figure out that card is supposed to contain a string.

Question :- Can we assign some other value to the existing variables ?

If we are reassigning an existing variable, a new value, we do not have to use the colon equals anymore. So, for example, if we re-assign the value “Five of diamonds” to the variable card, we can do so by just using an equal operator.

Question :- What shall happen, if we assign the different value to the existing variable ?

  • Since, GoLang is statically-typed language, we can’t assign a different type of value to the existing variable.

Question:- Explain the Tools of a GO program ?

Answer:- Let’s take a look at the following ToolSet provided by GO language. Go comes with a tool called Go. You use the Go tool for most everyday tasks such as building, testing, benchmarking, and starting third party packages and more :-

Compile and Run → In the above program, Behind-the-scenes, Go tool does following things :-

  • To compile your program and then run it.

Build → With above snapshot, we demonstrate, how do we build an executable that we can distribute.

  • This is done with the go build command. Go build Welcome.go. And Enter. You created a file called welcome.

Help → The Go tool can execute several other commands. To see all of them, run go help. Let’s go over some of these commands.

  • test → It will run the tests. Go has a built-in test suite. Test can also run benchmarks so you’ll be able to measure performance of your code.

Question:- Explain the Numbers and Assignments inside the GO program ?

Answer:- Let’s take a look at the following program to compute the Median with INTEGERS :-

  • We declare two variables, x and y, of type int. And unlike C or Java, the type comes after the variable name. In Go, you have int8, int16, int32, int64, and the unsigned versions of all of them. The int type, without size, depends on the system you are using, and is the one you will usually use.

Let’s take a look at the following program to compute the Median with FLOATING point numbers :-

  • At line nine and change the variable x to float64. There are two kinds of floats in Go, float64 and float32.

Question:- Let’s demonstrate the Random Number in Go-Lang ?

Answer:-

  • We can use a package called math/Rand and another package the time package that we’ve seen before. From the rand package, I’m calling the seed function and passing in the current time in Unix format.

Question:- Let’s demonstrate the Control-Flow through If-Else in Go-Lang ?

Answer:- We can use If-Else control flows like as follows :-

  • Notice that, we can’t use angle-brackets for putting the conditions for check.

Question:- Let’s demonstrate the Switch Loop in Go-Lang ?

Answer:- We do have switch case where decision is to be taken against the variable. Whatever it’s value is, basis of the same, the particular case is executed.

Question:- Let’s demonstrate the fallThrough scenario of Switch Loop in Go-Lang ?

Answer:- In case, we wanted to skip through the particular use-case, we can add the fallthrough keyword for that particular case.

Note here that, though the value of the variable “thisVar” is 1 and it should had come to the case 1, but all the cases are having fallthrough clause and that’s why, all cases are skipped and therefore control logic comes to the last case.

Also, remember that, all the cases can’t be skipped :-

Question:- Explain once more, how does If-Else Conditionals work with GO program ?

Answer:- There are two ways to specify conditions in Go : if and switch. Let’s start with if :-

  • In line nine, I’m assigning 10 to the variable x. And then in line 8, I can ask if x is bigger than five. Unlike Java or C++, you don’t need parenthesis around the condition. And if you run it, go run if.go, we’ll see that x is big is being printed out.

Let’s now have a look at Switch Statement :-

  • We start with an initialization statement. In our case it’s just the value of x. And then we list the cases here.

Question:- Demonstrate the for loop in Go-Lang ?

Answer:- There are 2 famous ways of looping though the array in Go-Lang :-

Basic fundamental goes like this :-

Question:- Let’s now demonstrate the break keyword in Go-Lang ?

Answer:- The break and continue keywords works exactly the same way, as they work in JAVA/C++ languages.

  • break → It breaks the flow of the code and ends the loop.

Question:- Let’s now demonstrate the GOTO labels in Go-Lang ?

Answer:- The goto labels in Go-Lang can be used to directly bunzy jump to a particular statement, but be careful while using the same, as it can very soon create spaghetti code. Here, the same is demonstrated :-

Question:- Explain once more FOR Loop iteration with GO program ?

Answer:- There are multiple ways to specify FOR loops in Go :-

Question:- Explain concept of Strings in GO program ?

Answer:- Strings are immutable in Go i.e. they can’t be changed once created :

Question :- Explain the basic go data-types ?

Question:- Let’s demonstrate the Custom DataTypes in Go-Lang ?

Answer:- We can declare custom data-types in Go using “struct” keyword. We can use a struct to group and store multiple values.

  • At line #17 to 19, each value of a struct is technically known as a field.

Question:- Demonstrate the usage of struct, in GoLang ?

Answer:- Below method demonstrates the creation of custom defined structure :-

At line #9, we have created an object of struct type : “Trade” with 4 attributes in it.

  • At line #14 above, we have initialised an object of struct type : “Trade”.

Question:- Demonstrate the concept of Constructors with GoLang ?

Answer:-

  • If you’re coming from an object-oriented language, such as Python, Java, C++, and others, you are used to having a constructor or initializer method that is called when an object is created.
  • Here, we are validating the data. For example → If the symbol is empty, we return nil for no object and an error that the symbol can not be empty. We also check the volume, that it’s not negative and we check the price, that it’s not negative.

Here, Interesting fact to note down is that, Go’s compiler doesn’t escape analysis and will allocate trade object on the heap. Let’s now use this method, in order to create the object.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next part of this series with Hands-On with Redis-Cluster.

References :-

--

--

Software Engineer for Big Data distributed systems

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