Welcome! Let's talk about the nil value. nil is a predeclared identifier.
Just like the other predeclared identifiers such as True, False lung function int, float64 and so on.
so it can be used everywhere without importing any package. simply means nothing or something
undefined, uninitialized and so. let's take a look at how it's called.
In other popular languages, for example in Javascript it is known as null or in Python it is None
in Java it is null
in Ruby as in Go it is nill
So if we know about one of these languages you already know what I'm talking about.
But if you don't, no worries!
It's actually a very simple concept to understand. it will be especially crystal clear
when I'll be talking about the pointers afterwards.
Basically nil just means that there is no particular value exists, but nil itself is a value.
It's a special value which says that there is no value.
You know already that in Go go every uninitialized variable gets a zero value.
Right? there is zero value for all the pointer based types like pointers, slices, maps, interfaces and channels
are nil.
That means values belong to those types are not yet initialized to some proper value.
In Go new value can be untyped and typed depending on the context.
You'll see about that later.
You also learn about those pointer backed types afterwards.
However, It is good to know now when nil is used for, ok. OK, In Go new value is also extensively used for error handling.
For example let's say that a function returns an error value.
I'm going to talk about what an error value means in the next lecture so when you call that function and when it
returns a nil value as the error value.
Then it means that there wasn't an error and the function call was successful or if the error wasn't
nil then it means that there was an error.
To understand what I mean by this.
Let's take a look at the simple code snippets here.
As you can see here I'm calling a function named do.
And it returns an error value.
So I am saving it into variable name "err".
So what should I do with this error value.
Let's check it out.
Here I'm just saying that when err variable isn't nill, then lets run the code inside the if statement here. let's
say that the err variable wasn't nill so the code inside the if statement will be executed.
Right, this means that there is an initialized value inside the error variable because it wasn't nill
the "do" function here has initialized that err variable.
It's an error value and then it returned it.
So since there was an error this means that calling "do" function was unsuccessful.
OK, it couldn't do its job.
There was a problem.
That's why
It has returned and non-nil err value.
So if that's the case then I need to handle error writing inside the if statements.
OK, then maybe I can lock that error or I can terminate from the program.
Otherwise let's say that the err variable was nil.
So that means there was no error! "do" function was successful so it returned a nil error value.
This means that I can continue executing my program because there wasn't an error.
So in summary, when there is an error.
You should always handle it.
one common way of doing it is just by using a simple if statement as in here.
Then you can take proper actions.
For example you can terminate from the program or if there wasn't an error.
You can happily continue executing the rest of the statements in your program.
All right.
This was a quick introduction to nil values and handling errors in Go. It is very normal if you don't
understand it now.
But with further examples.
nil and error handling will be much more clear.
No worries! In the next lecture.
I'm going to talk about error values and how to handle them using a real world code.
Thank you for watching! Bye Bye!.