Lesson 3
Understanding Basic Data Types in Go
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:

Go
1var daysInWeek int = 7 2fmt.Println(daysInWeek) // This will print: 7 3 4var maxInt64 int = 9223372036854775807 5fmt.Println(maxInt64) // This will print: 9223372036854775807

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:

Go
1var pi float64 = 3.1415926 2fmt.Println(pi) // This will print: 3.1415926
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:

Go
1var isEarthRound bool = true 2fmt.Println(isEarthRound) // This will print: true 3 4var isEarthFlat bool = false 5fmt.Println(isEarthFlat) // This will print: false

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:

Go
1var firstLetterOfAlphabet byte = 'A' // must be surrounded by SINGLE quotes 2fmt.Println(firstLetterOfAlphabet) // This will print: 65 – the code for "A"

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.

Go
1var welcome string = "Welcome to Go!" 2fmt.Println(welcome) // This will print: Welcome to Go!

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:

Go
1var unknown *string = nil 2fmt.Println(unknown) // This will print: <nil>

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.