Go Variables
Go Language - Variables
Variable can be defined as a named storage location in a memory. A variable is interpreted as the address to the location that holds a value that is stored in the variable. Variable can have data types which specifies the type, size of values that can be store in the variable.
A variable can be made of letters, numbers and underscore character. Variables are also case sensitive which means uppercase and lowercase letters are treated differently. It is one of the fundamental token in the programming language.
The syntax to declare a variable is :
Syntax
var variable_list datatype
Here, the data type is optional as Golang can assign the datatype at the time when the variable is initialized. In the variable list, the variables are separated from each other by commas.
Example :
Declaring a variable with a specific datatype ensures the compiler that the variable will be initialized later in the program, failing which anerror will be thrown. This is called Static Type Declaration.
Example :
Declaring a variable without specifying the datatype will force the compiler to interpret the datatype based on the value passed on to the variable. This is called Dynamic Type Declaration. But when initializing the variable, a colon should precede the = operator.
Example :
Golang also allows multiple declaration in a single line.
Example :
var a, b, c = 1, 2, “hello”
Ivalues and Rvalues in Go :
Ivalue is referred to the expression that refers to the memory location,which is the variable. These can be placed either in the left or ride side.
Example :
Syntax
var a = 10
var b = a
Rvalue is referred to the data value that is stored in the memory location. These can only be placed on the right side.
Example : var a=10 and not var 10=a