what’s going on everybody welcome to
what’s going on everybody welcome to part 20 of the go language programming
part 20 of the go language programming
part 20 of the go language programming tutorial series in this tutorial what
tutorial series in this tutorial what
tutorial series in this tutorial what we’re going to be talking about is the
we’re going to be talking about is the
we’re going to be talking about is the defer statement in ingo so what we’re
defer statement in ingo so what we’re
defer statement in ingo so what we’re talking about in the last tutorial was
talking about in the last tutorial was
talking about in the last tutorial was hey we’re using this kind of weight
hey we’re using this kind of weight
hey we’re using this kind of weight group and all that and it’s really cool
group and all that and it’s really cool
group and all that and it’s really cool but what if we run into something that
but what if we run into something that
but what if we run into something that causes this function to error and we we
causes this function to error and we we
causes this function to error and we we don’t actually get to tell the weight
don’t actually get to tell the weight
don’t actually get to tell the weight group hey we’re done well then we’re
group hey we’re done well then we’re
group hey we’re done well then we’re gonna have a problem we’re gonna be
gonna have a problem we’re gonna be
gonna have a problem we’re gonna be waiting all right and we don’t want that
waiting all right and we don’t want that
waiting all right and we don’t want that so this isn’t a problem that you’re
so this isn’t a problem that you’re
so this isn’t a problem that you’re gonna come across with just doing go
gonna come across with just doing go
gonna come across with just doing go routines and weight groups for example
routines and weight groups for example
routines and weight groups for example when you want to close connections when
when you want to close connections when
when you want to close connections when you’re done or close files or whatever
you’re done or close files or whatever
you’re done or close files or whatever if you hit an error before you get to
if you hit an error before you get to
if you hit an error before you get to the clothes you’re never gonna actually
the clothes you’re never gonna actually
the clothes you’re never gonna actually run that operation so in go they have
run that operation so in go they have
run that operation so in go they have the defer statement so the idea of defer
the defer statement so the idea of defer
the defer statement so the idea of defer is it’ll be evaluated whenever it gets
is it’ll be evaluated whenever it gets
is it’ll be evaluated whenever it gets hit but actually the defer statement
hit but actually the defer statement
hit but actually the defer statement won’t run whatever’s being deferred
won’t run whatever’s being deferred
won’t run whatever’s being deferred until the surrounding function either
until the surrounding function either
until the surrounding function either completes or panics out so if the if
completes or panics out so if the if
completes or panics out so if the if once it’s been evaluated if that
once it’s been evaluated if that
once it’s been evaluated if that function does happen to panic out all of
function does happen to panic out all of
function does happen to panic out all of the deferred functions will run so if
the deferred functions will run so if
the deferred functions will run so if it’s an error or if it doesn’t hit an
it’s an error or if it doesn’t hit an
it’s an error or if it doesn’t hit an error basically whenever that function
error basically whenever that function
error basically whenever that function is all set and done it’s gonna go ahead
is all set and done it’s gonna go ahead
is all set and done it’s gonna go ahead and run whatever you decided to defer so
and run whatever you decided to defer so
and run whatever you decided to defer so to exemplify this I’m gonna go ahead and
to exemplify this I’m gonna go ahead and
to exemplify this I’m gonna go ahead and clear clean up here we’ll come back to
clear clean up here we’ll come back to
clear clean up here we’ll come back to this you can I would recommend you save
this you can I would recommend you save
this you can I would recommend you save this because I’m just going to copy and
this because I’m just going to copy and
this because I’m just going to copy and paste it over when we return back to
paste it over when we return back to
paste it over when we return back to this but I just want to use a more
this but I just want to use a more
this but I just want to use a more simple example for now so let’s say
simple example for now so let’s say
simple example for now so let’s say we’ve got a func we’ll call it foo and
we’ve got a func we’ll call it foo and
we’ve got a func we’ll call it foo and foo basically just does a format dot
foo basically just does a format dot
foo basically just does a format dot print line and you know this is a
print line and you know this is a
print line and you know this is a function that does some stuff who knows
function that does some stuff who knows
function that does some stuff who knows right and then funk mein can run Fuu
right and then funk mein can run Fuu
right and then funk mein can run Fuu okay so if we wanted to we could we
okay so if we wanted to we could we
okay so if we wanted to we could we could say okay while this function is
could say okay while this function is
could say okay while this function is doing some stuff we want to make sure
doing some stuff we want to make sure
doing some stuff we want to make sure when it’s done doing some stuff we want
when it’s done doing some stuff we want
when it’s done doing some stuff we want to make sure it runs format dot print
to make sure it runs format dot print
to make sure it runs format dot print line done so this is gonna be evaluated
line done so this is gonna be evaluated
line done so this is gonna be evaluated immediately but it’s only gonna run when
immediately but it’s only gonna run when
immediately but it’s only gonna run when the rest of the foo function is done so
the rest of the foo function is done so
the rest of the foo function is done so let me pull this up and let’s run that
let me pull this up and let’s run that
let me pull this up and let’s run that real quick and we can see yeah it says
real quick and we can see yeah it says
real quick and we can see yeah it says doing some stuff who knows what and then
doing some stuff who knows what and then
doing some stuff who knows what and then it’s done running now what if we added
it’s done running now what if we added
it’s done running now what if we added another like what if we threw in another
another like what if we threw in another
another like what if we threw in another defer statement so we should so let’s do
defer statement so we should so let’s do
defer statement so we should so let’s do [Music]
[Music]
[Music] are we done so the way to first
are we done so the way to first
are we done so the way to first statements work is in a first in last
statements work is in a first in last
statements work is in a first in last out order I’m not really sure why they
out order I’m not really sure why they
out order I’m not really sure why they why it’s that way but basically that
why it’s that way but basically that
why it’s that way but basically that also means last in will be first out so
also means last in will be first out so
also means last in will be first out so what we’re gonna see when we run this is
what we’re gonna see when we run this is
what we’re gonna see when we run this is doing some stuff
doing some stuff
doing some stuff are we done and then done so for just to
are we done and then done so for just to
are we done and then done so for just to show that right sure enough that’s
show that right sure enough that’s
show that right sure enough that’s that’s how it ran so we could also
that’s how it ran so we could also
that’s how it ran so we could also exemplify that with like a for loop or
exemplify that with like a for loop or
exemplify that with like a for loop or something like that so for example we
something like that so for example we
something like that so for example we could we could say for I colon equals 0
could we could say for I colon equals 0
could we could say for I colon equals 0 Wow I less than 5 we could just say
Wow I less than 5 we could just say
Wow I less than 5 we could just say defer format print line high so save
defer format print line high so save
defer format print line high so save that run it and sure enough it basically
that run it and sure enough it basically
that run it and sure enough it basically counts down right so getting back to our
counts down right so getting back to our
counts down right so getting back to our main example I’m just gonna kind of copy
main example I’m just gonna kind of copy
main example I’m just gonna kind of copy and paste it over copy paste so this is
and paste it over copy paste so this is
and paste it over copy paste so this is our original example where we kind of
our original example where we kind of
our original example where we kind of came into this so this one it works and
came into this so this one it works and
came into this so this one it works and it waits so just to show right it just
it waits so just to show right it just
it waits so just to show right it just runs those two go routines waits for
runs those two go routines waits for
runs those two go routines waits for them to finish but if wait group done
them to finish but if wait group done
them to finish but if wait group done never runs we’re in trouble so
never runs we’re in trouble so
never runs we’re in trouble so what we could say is rather than running
what we could say is rather than running
what we could say is rather than running WG gun down here we’ll just cut that and
WG gun down here we’ll just cut that and
WG gun down here we’ll just cut that and then we defer wait group done to the top
then we defer wait group done to the top
then we defer wait group done to the top so then we can save that and and now we
so then we can save that and and now we
so then we can save that and and now we can rerun function or the program and
can rerun function or the program and
can rerun function or the program and now we’ve got hey there hey there hey
now we’ve got hey there hey there hey
now we’ve got hey there hey there hey there okay so that is how we can defer
there okay so that is how we can defer
there okay so that is how we can defer things and why we might want to defer
things and why we might want to defer
things and why we might want to defer things and if we’re going to be talking
things and if we’re going to be talking
things and if we’re going to be talking about the defer statement we might as
about the defer statement we might as
about the defer statement we might as well also talk about panic and recover
well also talk about panic and recover
well also talk about panic and recover in the go programming language so while
in the go programming language so while
in the go programming language so while we can use defer to make sure something
we can use defer to make sure something
we can use defer to make sure something happens we can also use defer to recover
happens we can also use defer to recover
happens we can also use defer to recover after a panic has occurred so so you
after a panic has occurred so so you
after a panic has occurred so so you could defer some sort of function that’s
could defer some sort of function that’s
could defer some sort of function that’s gonna handle for whatever the error was
gonna handle for whatever the error was
gonna handle for whatever the error was that we might have actually hid that way
that we might have actually hid that way
that we might have actually hid that way your program doesn’t stop running or at
your program doesn’t stop running or at
your program doesn’t stop running or at least it doesn’t have to you could let
least it doesn’t have to you could let
least it doesn’t have to you could let it stop running but you don’t have to do
it stop running but you don’t have to do
it stop running but you don’t have to do that anyway
that anyway
that anyway in the next tutorial that’s we’re gonna
in the next tutorial that’s we’re gonna
in the next tutorial that’s we’re gonna be talking about we should have no
be talking about we should have no
be talking about we should have no problem covering both panic and recover
problem covering both panic and recover
problem covering both panic and recover in the next tutorials so panic is a way
in the next tutorials so panic is a way
in the next tutorials so panic is a way that we can say hey we’ve hit an error
that we can say hey we’ve hit an error
that we can say hey we’ve hit an error based on some sort of condition so we
based on some sort of condition so we
based on some sort of condition so we can panic out but also like I was saying
can panic out but also like I was saying
can panic out but also like I was saying defer doesn’t necessarily only run if
defer doesn’t necessarily only run if
defer doesn’t necessarily only run if the function successful defer will also
the function successful defer will also
the function successful defer will also all the deferred statements are gonna
all the deferred statements are gonna
all the deferred statements are gonna run if the function also panics out so
run if the function also panics out so
run if the function also panics out so if the surrounding function panics out
if the surrounding function panics out
if the surrounding function panics out all the deferred functions will also run
all the deferred functions will also run
all the deferred functions will also run so then within within those deferred
so then within within those deferred
so then within within those deferred functions we can recover from panics so
functions we can recover from panics so
functions we can recover from panics so anyways that’s what we’re going to talk
anyways that’s what we’re going to talk
anyways that’s what we’re going to talk about in the next tutorial if you have
about in the next tutorial if you have
about in the next tutorial if you have questions comments concerns whatever
questions comments concerns whatever
questions comments concerns whatever feel free to Livan below otherwise I’ll
feel free to Livan below otherwise I’ll
feel free to Livan below otherwise I’ll see you in the next tutorial
Be First to Comment