Hello Readers! Today in this blog we will see different Data types and Variables in Go. And how easily we can use them. So, stay tuned with me!
Firstly we will see what a variable actually is? In programming, we have variables, it is essentially some way of storing and accessing information. It holds some value for us; we may manipulate that variable. Or We can change it later also. We can use it somewhere.
- Variable names are case sensitive.
- You cannot use keywords such as func, import, etc as the name of a variable.
- A variable name should not start with a digit.
- A variable name should begin with a letter or an ‘ _ ‘ underscore. Here are some examples of valid variable names:
_variable1 , John , myvariable , tim_name2
By using var keyword we can declare a variable. Its syntax is:
var var_name type = expression
In this syntax, either type or = expression can be used, but not both.
Lets see the following use cases the example how we can use different variables. Here, the first line will print the value of the variable and the second line will print the type of the variable in the output.
use integer in variable:
Here I am assigning integer value in the variable.
use string in variable:
Here I am assigning string value in the variable.
use float in variable:
Here I am assigning float value in the variable.
use boolean as variable:
Here I am assigning boolean value in the variable.
Here the output is default value which is false for boolean.
So, these are the variable declaration with initial value. Let’s see how we can declare variables without initial value.
Variable Declaration Without Initial Value
Usually, we use to initialise the variable. So, in the case if I declare a variable without initial value then it will take default value as output. Let’s see in following example:
In the output after running, we can see here that they already have the default values according to their types.
Multiple Variable Declaration
We can also declare variables in a single line.
Basically Data types specify the types of variables in your program. So, it can be categorized into following types:
Unsigned Integers (no negatives)
- uint8/byte (0 to 255)
- uint16 (0 to 65535)
- uint32 (0 to 4294967295)
- uint64 (0 to 18446744073709551615)
Signed Integers (negatives)
- int8 (-128 to 127)
- int16 (-32768 to 32767)
- int32 (-2147483648 to 2147483647)
- int64 (-9223372036854775808 to 9223372036854775807 )
3 Machine Dependent Types:
- uint (34 or 64 bits)
- int (same size as unit)
- uintptr (an unsigned integer to store the uninterpreted bits of a pointer value)
Floating Point Numbers:
- float32 (IEEE-754 32-bit floating point numbers)
- float64 (IEEE-754 64-bit floating point numbers)
Complex (Imaginary parts)-
- complex64 (complex numbers with float32 real and imaginary parts)
- complex128 (complex numbers with float64 real and imaginary parts)
Lets see an example through following code:
Its output as you can see below:
So, that was all about variables and data types in Go.
Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.