Press "Enter" to skip to content

Panic and Recover – Go Lang Practical Programming Tutorial p.21

what is going on everybody welcome to
part 21 of the go language tutorial
series in this tutorial what we’re
talking about is the panic and recover
functions in goaline in the previous
tutorial we talked about the defer
statement which basically defers the
running of a function until the end of
the surrounding function is met either
that’s the functions done or if the
function does happen to err panic out
the deferred statements will run so
we’re talking about panic and recovery
now because one panic is not just
something that happens on its own and
you can actually initiate a panic
yourself if certain conditions are the
case but then if you do panic naturally
the program will just stop running and
generally we like that often to not be
the case that the program stops we’d
like some way to recover and that’s what
the recover function is for so and
generally the recover function you’re
probably going to put inside of one of
the deferred functions so let’s kind of
see how all this works so in the
previous tutorial this is the code that
we finished with and I know all you guys
that were following along were like but
Harrison what if I was equal to two
obviously that’s a big deal so if I is
equal to two we obviously need to throw
it hair right so we’re gonna panic and
of course we’re gonna panic and we’re
just gonna pass oh dear a two so this
you you have to pass something and what
we’re gonna pass is just the string
message basically that just says uh we
got a two okay so so we can actually
save this and for now let’s just run and
see what this looks like Oh what did I
do this is not that the this issue that
I wanted to see I’m pretty sure right
non declaration oh we know
come on in if I is equal to two right so
if I is equal to 2 which we hit really
we’re gonna hit it twice but we first
hit it over here we panic oh dear a two
and then everything just falls apart at
this point which is obviously not
exactly what we wanted so now we want to
do is we want to be able to recover we
want to be able to recover from this
this panic so let’s say instead let’s
create a new function so we’re gonna do
funk so just for the record you can
write like functions in line so you
could defer a function right here but I
don’t really like doing that so I’m
gonna create a new function and what I’m
gonna do is we’re gonna call this
function clean up and inside of clean up
the first thing that we’re gonna ask is
if our colon equals recover so here
we’re just assigning a value to our we
could do it up before this but we could
also do it in line with the if the if
statement so if our if our does not
equal nil so when we ask to recover if
there’s not been any sort of panic
situation it’s gonna return nil so
nothing but if it doesn’t if it if it’s
not nil then we want to recover the
situation so we’re gonna do format dot
print line and the recovery that we’re
gonna do is we’re just going to say
recovered in cleanup function basically
and then we also want to know whatever
our is our is gonna be the panic in this
case it’s just gonna be oh dear or two
but you could a court you could panic
all kinds of things this is just a
really simple example so that’s our
cleanup so we could also defer cleanup
and throw that in there as well
so let’s go ahead and yeah save that
come over here and then now let’s run
that and as you can see what happens is
we’re able to actually you know it says
hey we recovered in the cleanup function
and then this is what the actual error
was oh dear or two and in fact mm-hmm I
kind of want to do this just to make it
a little more clear what’s going on
whoops all right there we go
okay so that’s how we can do a recover
and also you know obviously we’re
stacking these defer statements so again
cleanup is actually running before
weight group done runs now in this case
I think we should probably add weight
group dot done here in the cleanup it
seems to make the most sense to me you
can stack two first it’s not really
anything that is necessarily gonna be
frowned upon I just think it makes a
little bit of sense to throw it in the
cleanup like that so then we can save
that we should see that everything runs
the same you also could in theory have
defer inside here okay we could still do
that probably w/g done we’re gonna be in
trouble if this doesn’t run and but to
be honest with you I haven’t spent
enough time with go to know whether or
not it would be totally safe to not
defer seems like or or what kind of
repercussions we could face if we throw
in and defer there so if you know the
answer whether or not it’s a bad idea to
to do defer weight group done let me
know I feel like if we do it this way we
defer and then we also defer this I’m
not really sure there’s any negative
benefit here or negative impact negative
benefit anyway I’m not really sure
there’s any sort of negative impact by
doing it this way and we really need to
make sure we say we’re done to the
weight group so I kind of feel like it’s
probably best if you pass into fur
weight group done either here or
you could do it in this function it just
looks a little cleaner to me to say hey
let’s do in the cleanup
mostly because I think if you stack the
defers it can make it cutting to read
Because defers first of all they happen
generally in the beginning of a function
but then they don’t actually do anything
until the end and then but also the
stacked defers go in reverse order so it
can be kind of confusing anyways yeah if
you have any suggestion on what’s the
cleaner way to do things go ahead and
let me know also I just happen to catch
this people are complaining or were
complaining again you can always run go
format and you can get all the fixes to
your silly code but yeah I’ll try to fix
it as I see mistakes but feel free to
point them out too
anyway that’s it for now that is defer
panic recover all kinds of good stuff
in the next tutorial what we’re gonna be
talking about is we’re gonna get back to
concurrency because remember our you
know what we’re trying to work on is
apply this to our web app and we can
obviously use go routines with our web
app but we need one more thing right
like the go routines they were going off
and running and doing their own kind of
thing but what we need is go routines
that are going to return values and in
in and somehow continue the concurrency
without any sort of overwriting and
corruption going on so the way that
we’re going to do that is by using
channels so channels are ways to send
and receive values with go routines so
that’s we’re going to be talking about
in the next tutorial if you have
questions comments concerns whatever
feel free to them below or improvements
otherwise I’ll see you in the next
Please follow and like us:

Be First to Comment

Leave a Reply