Let’s GO! Part 6: Basic Types
Types in GO divide into four categories:
- Basic Types
- Aggregate Types
- Reference Types
- Interface Type
We’ll cover all of them one by one in separated parts.
First and foremost:
The Basic Types
The basic types in GO divide into 3 categories themselves:
- Numbers
- Booleans
- Strings
Numbers
We have Integers, Floating Points and Complex numbers. Here’s some examples of each one:
Integer:
1
56
-4654Floating Points:
1.2
-4.2467
3.14159265359Complex:
(6+4i)
(1.23+32.6555i)
(-3-5i)
Here is a table that shows you all of the info you need for numeral data types. You don’t need to memorise them! Just come back here whenever needed:
If you create a variable with any of the numeral types and assign no values to it, the default value will be 0
for all of them except the complex types, and (0+0i)
for the complex types.
Booleans
There’s only one boolean type that is named bool
. It consists of one single bit representing either true
or false
. One important note about this type is that it cannot be converted implicitly or explicitly to any other types; So be careful with it! So for example you could do something like true == 1
in Javascript which it would’ve returned true
; but GO doesn’t let you do that in the first place. Eventually having conditional statements (which we’ll cover in a few parts later on) with non-boolean conditions like if 0 {}
or if 1 {}
or even if nil {}
won’t work in GO while the concept works in some other languages like Javascript.
If you create a variable with type bool
and assign it no values, the default value would be false
.
Strings
Strings in GO are declared between double quotes and not single quotes. They represent UTF-8
immutable bytes and are named string
. The most basic function you can run on a string is len(varName)
which will return the amount of characters in that specific string. If you wish to know more about the strings in GO, here’s another good and detailed article about them:
If you create an string without assigning a value to it the default value would be an empty string.
Alright you did it! You just learned about the Basic Types in GO. Please don’t memorise anything as that is not what a good software engineer would do. Refer to these articles as you go and PLEASE practice the things you learn. That is the only way you can become a worthy GO developer.
Next we’ll learn about the Aggregate Types. Buckle up!