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