Go Variables
A variable is a name given to a particular area in memory, that stores a value, which can later be reused. Variable names in Go can contain letters, numbers and underscores, but must begin with either a letter or underscore. Variable names are also case-sensitive. Although underscores are allowed in variable names, by convention mixed case is used, for example, 'firstName' or 'FirstName', rather than 'first_name' or 'First_Name'. It is possible to declare a variable with or without specifying the type of data that it can store, such as string, integer or floating point number. Where no data type is specified, Go allocates a type based on the data that is assigned to it.
var variableName type = value
Where the data type is specified, a variable doesn't need to be initialised when it is declared.
var variableName type
variableName = value
A variable can be declared without specifying the data type, in one of two ways. The data type is determined based on the value being assigned.
var variableName = value
The second way of declaring a variable without the data type omits the 'var' reserved word.
variableName := value
It should be noted that this method is only available for use from within a function.
Below are examples variables, all of type 'string', showing these methods of variable declaration and initialisation. It should be noted that when a string variable is declared without being initialised, it is given a default value of an empty string.
var exampleString1 string = "This is string 1" var exampleString2 string exampleString2 = "This is string 2" var exampleString3 = "This is string 3" exampleString4 := "This is string 4"
The 'bool' data type stores Boolean values, 'true' or 'false'. If a Boolean variable is declared without being initialised, it is given a default value of 'false'.
var exampleBoolean1 bool = true
There are a number of different numerical data types, which can be grouped into three categories, integers, or whole numbers, floating point numbers, and complex numbers. Integer data types include, 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'int', 'uint', and 'uintptr'. The number at the end of each data type refers to the size in bits. Those with a 'u' at the start indicate an unsigned integer, whilst those without are signed integers.
Floating point data types include 'float32' and 'float64', again with the number at the end referring to the size in bits.
Complex number data types are like complex numbers in mathematics, they are made up of two parts, the first represents a real number, whilst the second represents an imaginary number. There are two complex data types, 'complex64' and 'complex128', which are 64 and 128 bits in size respectively.
Example integer, float and complex variables can be seen below.
var exampleInt1 int = 5 var exampleFloat1 float32 = 5.4 var exampleComplex1 complex64 = complex(5, 4)
The values of variables can be displayed, for example, in a console or terminal window.
var exampleString1 string = "This is string 1" var exampleBoolean1 bool = true var exampleInt1 int = 5 var exampleFloat1 float32 = 5.4 var exampleComplex1 complex64 = complex(5, 4) fmt.Println(exampleString1) fmt.Println(exampleBoolean1) fmt.Println(exampleInt1) fmt.Println(exampleFloat1) fmt.Println(exampleComplex1)
This will display the following.
This is string 1 true 5 5.4 (5+4i)
It is also possible to display the variables as part of a larger string.
fmt.Println("Value of variable exampleString1:", exampleString1)
fmt.Println("Value of variable exampleBoolean1:", exampleBoolean1)
fmt.Println("Value of variable exampleInt1:", exampleInt1)
fmt.Println("Value of variable exampleFloat1:", exampleFloat1)
fmt.Println("Value of variable exampleComplex1:", exampleComplex1)
The below output is produced.
Value of variable exampleString1: This is string 1 Value of variable exampleBoolean1: true Value of variable exampleInt1: 5 Value of variable exampleFloat1: 5.4 Value of variable exampleComplex1: (5+4i)