Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - VARIABLES


Variables in Go are storage location where the data(Be it a number or a Name) is stored.It is like a container where something is stored.

Example :

Let us go back to our school days. You are in the Maths class and your Maths teacher had asked you to add two numbers. i.e. She asked you to add the numbers 5 and 6 and tell her the result.

And you have written the numbers 5 and 6 in your notebook, added them and and told the result 11 to your teacher.

Well! Go takes the same step to add two numbers. The only difference is, you have used one notebook to write and add the numbers.


But Go uses different locations to store each number. And they are called as 'Variables'.


Let us see how 'Variables' are used in Go for addition of two numbers.


Go Code to add two numbers


Example :



package main
import "fmt"
    
func main() {
        
    var x = 5;
    var y = 6;
        
    var z = x + y;
    fmt.Println("The added value is :",z);
}    


Output :



  The added value is : 11

Now, if you look at the above code, 'x', 'y' and 'z' are 'Variables'. To say that 'x', 'y' and 'z' are 'Variables', there is a prefix of 'var' before the variables.


var x = 5;

Note : Semicolon ';', at the end of each line is totally optional. If you don't want to put semicolons, then don't.

Now, let's see the below steps to add two numbers.


Now, if you look at the above code, 'x', 'y' and 'z' are 'Variables'. To say that 'x', 'y' and 'z' are 'Variables', there is a prefix of 'var' before the variables.

  1. 'x' is holding the first number '5'.

    var x = 5;


    The above line says, create an empty location named 'x'. And assign it with the value '5'. And 'x' is called as 'variable'.
    java_Collections


    Just note that the '=' in the above code is called 'assignment operator'. It actually means the value '5' at the right should be placed in the storage location/variable 'x'.

  2. 'y' is holding the second number '6'.

    var y = 6;


    Again, the above line says, create an empty location/variable named 'y'. And assign it with the value '6'.
    java_Collections

  3. And 'z' is holding the added value of (5 + 6).

    var z = x + y;


    Similarly, the above line says, create an empty location/variable named 'z'. And store the added value to it.
    java_Collections

  4. Finally, we are printing the added value of (5 + 6) on the screen. Which is stored in 'z'.

    fmt.Println("The added value is :",z);


    The above Print(...) statement has two sections.

    First section has the text inside double quotes '"'.

    "The added value is :"


    And the second section does not have double quotes. And is usually a 'variable'.

    z


    And they are separated by a comma ','.

    "The added value is :",z


    The comma ',' in the above statement is a concatenate operator. i.e. It joins the first and the second section to form a meaningful sentence.

    And we get the below output :

    The added value is : 11

So far we have seen, how to store a number (i.e. x = 5) in a 'Variable'.


Now, let us see how to store a String(i.e. It could be a Name or a City) in a Variable.


Storing a String in a Variable


Let us say, we want to store the String 'Hello World' in a Variable(Say 'x').


And the process of storing a String is same as storing a number, but with a mild difference.


Below is the Go Code :


Example :



package main
import "fmt"
    
func main() {
        
    var x = "Hello World"
    fmt.Println(x);
}


Output :



  Hello World

So, in the above code, we have initialised the String 'Hello World' to the variable 'x'.


var x = "Hello World""

The only thing to note here is, if you are initialising a String (i.e. Hello World), it should be in Double Quotes (""). i.e. "Hello World".


java_Collections

Dealing with multiple variable and data


Let us say we have three variables and also three different values.


Interestingly, Go provides us the flexibility to assign three values to all three variables in a single line.


Below is the Go Code :


Example :



package main
import "fmt"
    
func main() {
        
    var x, y, z = "Hello", "Beautiful", "World";
    fmt.Println(x);
    fmt.Println(y);
    fmt.Println(z);
}


Output :



 Hello
 Beautiful
 World

So, in the above code, we have three variables 'x', 'y' and 'z'. And we have assigned the string 'Hello', 'Beautiful' and 'World' to all the three variables in a single line.


var x, y, z = "Hello", "Beautiful", "World";

java_Collections

The ':=' Operator


The ':=' Operator is a short way to declare and initialise a value to a variable.


i.e. We have initialised the number '5' to the variable x' using the below way,


var x = 5

Now, if you want to omit 'var', you can use ':='.


x := 5

Example :



package main
import "fmt"
    
func main() {
        
    x := 5	
    fmt.Println(x);
}


Output :



 5

Now, just think for a moment, you have initialised a number to a variable,


var x = 5 or x := 5

Or, a String to a variable,


var x = "Hello World" or x := "Hello World"

But, don't you think, if there could be a way to tell Go that a variable, say 'x', should only be able to hold a number and not a String.


i.e. The variable, 'x' would only contain a number.


var x = 5

And if you want to insert a String to the variable 'x', it would throw an error.


i.e. The statement


var x = "Hello World"

Would throw an error.


And luckily, that is possible in Go.


Allowing only integer type in a variable


If you want to make the variable 'x' to contain only a number. You need to declare it in the below way,


Example :



package main
import "fmt"
    
func main() {
        
    var x int = 5;
    fmt.Println(x);
}


Output :



 5

All we have done is, placed 'int' keyword after the variable 'x'.


java_Collections

And if you want to initialise a String to 'x'. It would throw an error.


var x = "Hello World"

Example :



package main
import "fmt"
    
func main() {
        
    var x int = "Hello World";
    fmt.Println(x);
}


Output :



 cannot use "Hello World" (type untyped string) as type int in assignment

And we get the above error, stating a String cannot be inserted in the variable 'x', as it is supposed to hold an integer only.


Similarly, if you want to make a variable hold only a String type value. You can do it in the below way.


Allowing only String type in a variable


Example :



package main
import "fmt"
    
func main() {
        
    var x string = "Hello World";
    fmt.Println(x);
}


Output :



 Hello World

Similarly, we have placed 'string' keyword after the variable 'x'.


java_Collections

Go Variable Naming Convention


Just like any other language, there is a rule to declare the 'Variables'.


Although, the 'Variables' can be of any names with some restriction.


Below are the rules :

  1. A lower case Variable is different from an upper case Variable.

    i.e. The lower case variable 'a' and upper case variable 'A' are different.

  2. A Variable should never start with a number.

    i.e. The Variable with name '2y' is never allowed.

  3. A Variable can contain alphabets (i.e. A to Z or a to z), numbers (i.e. 0 to 9) or an underscore (i.e. _).

So, if you have two numbers and need to assign them to two 'Variables'. You can either declare the variables as 'x' and 'y'.


var x = 5

And


var y = 7

But the preferred way to name the 'Variables' would be 'first_number' and 'second_number'.


var first_number = 5

And


var second_number = 7

But you can never use 'Variable' names with spaces like 'first number' or variables with a - like 'first-number'.