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.
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.
package main import "fmt" func main() { var x = 5; var y = 6; var z = x + y; fmt.Println("The added value is :",z); }
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.
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.
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.
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 :
package main import "fmt" func main() { var x = "Hello World" fmt.Println(x); }
So, in the above code, we have initialised the String 'Hello World' to the variable 'x'.
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".
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 :
package main import "fmt" func main() { var x, y, z = "Hello", "Beautiful", "World"; fmt.Println(x); fmt.Println(y); fmt.Println(z); }
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.
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,
Now, if you want to omit 'var', you can use ':='.
package main import "fmt" func main() { x := 5 fmt.Println(x); }
Now, just think for a moment, you have initialised a number to a variable,
Or, a String to a variable,
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.
And if you want to insert a String to the variable 'x', it would throw an error.
i.e. The statement
Would throw an error.
And luckily, that is possible in Go.
If you want to make the variable 'x' to contain only a number. You need to declare it in the below way,
package main import "fmt" func main() { var x int = 5; fmt.Println(x); }
All we have done is, placed 'int' keyword after the variable 'x'.
And if you want to initialise a String to 'x'. It would throw an error.
package main import "fmt" func main() { var x int = "Hello World"; fmt.Println(x); }
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.
package main import "fmt" func main() { var x string = "Hello World"; fmt.Println(x); }
Similarly, we have placed 'string' keyword after the variable 'x'.
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 :
So, if you have two numbers and need to assign them to two 'Variables'. You can either declare the variables as 'x' and 'y'.
And
But the preferred way to name the 'Variables' would be 'first_number' and 'second_number'.
And
But you can never use 'Variable' names with spaces like 'first number' or variables with a - like 'first-number'.