Press "Enter" to skip to content

Defer – Go Lang Practical Programming Tutorial p.20


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

Be First to Comment

Leave a Reply