A variable is nothing but a name given to a storage area that the programs can manipulate. Each variable in Go has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Go is case-sensitive.
Go programming language also allows to define various other types of variables such as Enumeration, Pointer, Array, Structure, and Union
var variable_list optional_data_type;
var i, j, k int;
var c, ch byte;
var f, salary float32;
d = 42;
There are two kinds of expressions in Go −
-
lvalue − Expressions that refer to a memory location is called "lvalue" expression. An lvalue may appear as either the left-hand or right-hand side of an assignment.
-
rvalue − The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right- but not left-hand side of an assignment.
The following statement is valid −
x = 20.0
The following statement is not valid. It would generate compile-time error −
10 = 20