All the data that is stored in memory is essentially just a set of bits. It is the type of data that determines how that data will be interpreted and what operations can be performed with them. The Go language is statically typed, meaning that all the data used in the program is of a specific type.
Go has several built-in data types and also allows you to define your types. Let's take a look at the basic built-in data types that we can use.
Type | Description | # In Memory |
---|---|---|
int8 | values from -128 to 127 | takes 1 byte in memory |
int16 | values from -32768 to 32767 | 2 bytes (16 bits) in memory |
int32 | values from -2147483648 to 2147483647 | 2 bytes (16 bits) in memory |
uint8 | values from 0 to 255 | 1 byte in memory |
uint16 | values from 0 to 65535 | 2 bytes in memory |
uint32 | values from 0 to 4294967295 | up to 4 bytes in memory |
It's easy to remember here that there are signed types (that is, those that can be negative) and there are unsigned positive types that start with the prefix u (uint32). And then there's byte, which is a synonym for uint8, and rune, which is a synonym for int32.
Note: the int and uint types. They have the most effective size for a particular platform (32 or 64 bits). This is the most commonly used type for representing integers in a program. Various Compilers can provide different sizes for these types, even for the same platform.
An example using floats type:
var x float64 = 5.7
There are two types to represent fractional numbers:
float32 and float64.
The float32 type provides six decimal precision digits, while the precision provided by the float64 type is about 15 digits.
A boolean or bool type can have one of two values: true or false.
var isTrue = true
var isFalse = false
If a variable is not assigned a value, it has a default value that is defined for its type. For numeric types, it's 0, for a Boolean type, it's false, for strings ""(empty line).
When defining a variable, we can omit the type if we explicitly initialize the variable with a value:
var website= "techiclues"
In this case, the compiler implicitly derives the variable type from the value. If a string is assigned, then the variable will represent the string type, If an integer is assigned, the variable represents the int type, and so on.
The same thing essentially happens when a variable is briefly defined when the data type is also not explicitly specified:
var website := "techieclues.com"
Note : it should be borne in mind that if we do not specify a type for a variable, then it must be assigned some initial value. declaring a variable without specifying the data type and seed value at the same time would be an error.