In the previous blogs, we have covered enough topics to get you the introductory knowledge about How to code in Go. It is now time that we dive deep into specific topics and understand the in and out's.
In this blog, we will go through how to declare variables, assign values to variables, and then how to do both of them at the same time. Then we will discuss about the differences between the styles and when should you use which one.
Style 1 : Using var Keyword
The first and foremost way we will cover is using the var keyword. We have use this method in our previous introductory blogs.
We declare a variable using the following syntax:
var <name> <type>
Here, we first write the keyword var followed by the name of the variable and then the type we want to use. For example, we want to declare a variable called title of type string. It will look like this:
var title string
This way, we do declare the variable title but we don't assign a value to it at this moment. It is initialized or assigned it's default or zero-value which is empty string. You can verify this by printing the value of title using the fmt.Println method.
Style 2 : Using var Keyword but also Assigning a Value.
Adding to our style 1, we can further simplify the process by declaring and assigning the value at the same time. We do so in the following way:
var title string = "Spaces or Tabs"
We will not discuss about Spaces vs Tabs here but do comment your preference in the comment section.
In style 2, we are declaring the variable as we did in style 1 but we also added = <value>
after it. This way, we can declare a variable and assign value to it immediately. Now, let's move onto style 3.
Style 3 : Using the short-declaration Operator.
The third style in which we can declare and assign an initial value to our variable is using the short-declaration operator.
We do it in the following way:
title := "Spaces or Tabs"
Those who have experience in python may recognize it as the walrus operator.
In this style, Go itself inferences the type of our variable by looking at the value assigned on the right hand side.
You might think like "Shouldn't we use style 3 all the time."
We do use it most of the time but there are scenarios where we have to use either style 1 or style 2.
Differences and When to Use.
Let's first discuss about the differences this styles comes with.
There are no differences in style 1 and style 2 but if you are aware of any, please share it for others in the comment section.
The main difference style 3 has with style 1 and style 2 is that we are not telling Go the type we want to use with the variable. Now, since Go itself is attaching the type to our variable, there are some limitations. In blog #3 Primitive Data Types in Go, we discussed about the different types of int present in Go. We have uint. Within uint, we have uint8, uint16, uint32 and uint64. Same goes for int, floats, and complex numbers.
When we define a variable like age := 18
, Go initializes the variable age with value 18 of type int.
When we run the program, we see that our variable age is of type int.
Now, the problem arises when we want to declare the variable with different type of integer other than int like int8 or uint8. In this scenario, we use style 1 or style 2.
When to use which style.
Short-declaration operator can be used only inside a function body.
To define global variables or declare variables out of function blocks, we use the style 1 or style 2.
When we want to attach a specific type like uint8 or uint32, we use the style 1 or style 2.
With that said, we will wrap up our post here.
More on scope of go code will be discussed in the later blogs.
Make sure to follow me on DEV or Twitter if you like the content. It will help in increasing the reach of blogs to a wider audience.
Top comments (0)