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!