Introducing Numerical Data Types

In Go, we use numerical data types to represent numbers. Specifically, in this lesson, we're focusing on int and float64. The int data type represents whole integer numbers, and the float64 data type signifies decimal numbers — numbers with a decimal point.

The largest value an int can store depends on the system. It's 2322^{32} (2147483647) on a 32-bit system and 2642^{64} (9223372036854775807) on a 64-bit system.. Here's an example of using the int number:

Now, let's move on to the float64 data type. We use float64 when dealing with numbers that have decimal points, also known as floating-point numbers. It provides a precision of 15–17 digits. Consider the following example:

Discovering Boolean and Byte Data Types

Let's now shift focus to the bool and byte data types.

The bool data type in Go can hold one of two possible values: true or false. This data type receives extensive use in logical expressions and decision-making. Here's a simple example:

The byte data type is a special type of integer. Each symbol (character) is associated with some code. With byte data type we can store a symbol's code in a variable.Here's how to use it:

Note that in Go there is no special data type for storing characters. If you want to store a single symbol, store it inside the string data type. Let's explore it.

Exploring String Data Type

You'll find that the string data type is as common in Go as there are stars in the cosmos. Go treats string as a basic data type and uses it to store a sequence of characters — just a piece of text. The string is always surrounded by double quotes.

Interestingly, string in Go is immutable. Once a string is created, we cannot change its value.

Understanding nil

As we conclude this journey, we will discuss a very special value: the nil value. nil means "no value" or "nothing", or "unknown". It's not equivalent to an empty string ("") or 0. You can't assign nil to a regular variable. But you can assign a pointer to nil. Pointer is a variable that holds not the value itself, but a link to the value. It effectively points to a specific place in RAM where the required value is stored. We will explore pointers later, by now let's use it to see nil in action.

Here's how you assign nil to a pointer:

While string is a data type that stores strings, *strings is a pointer to a place where we expect to find a string. But in this case, our pointer points nowhere.

Note: As nil is nothing, you can't perform any operations on it. You can still print the nil variable or reassign it to an actual value, but you can't perform any other operations on it. Attempting to do so will cause an error known as nil pointer dereference. But no worries, we will cover this in detail in subsequent lessons!

Lesson Recap and Practice Announcement

Bravo! You've successfully navigated the basic data types in Go. You can now use int and float64 for numerical computations, bool for decision-making, byte to represent Unicode code points, string to handle texts, and nil to represent an unknown value.

Although we've covered a lot, more practice is coming. This additional practice aims to further solidify your understanding. So, get ready for the exercises designed to put your newfound knowledge to the test!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal