Golang Error Handling - ULTIMATE Golang Basics Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey error handling is pretty damn important in gold so make sure to pay attention in today's video something interesting welcome back here to the golang dojo this channel is all about the go programming language and becoming golang ninjas together so make sure to subscribe if that's something you fancy if you have done any research on a golang you would know that error handling is quite different in golang compared to other c like languages like java and cnc plus plus where when you have an error you would typically throw an exception and then you have a try catch a blog in order to handle the exception where whether it's a checked exception or unchecked exception however in go you would just return an error like you would return any function result and then you would inspect on the function result that includes an error to check if an error is null or not if we go ahead and create a function a simple function that does nothing but return and by turning an error obviously when you're writing production code you're probably not going to have a function like this and the function is going to return one result and that result is of type error and this function is going to return and called the errors built-in library and specifically the new method function inside of those arrows a built-in library and this new function will take in a string as a parameter we'll give it some error message here and it will give me a warning because there's some format in the string that in the error string that we want to follow as you can read here the error string should not be capitalized or end with a punctuation mark it's just how it is and go so we can go ahead and remove the capitalization and the punctuation and this is how you would write a function and that returns an error in go now we are returning an error how can we actually handle the error that's the gist of this in today's video right so we will go ahead and assign the value of this function to there's a variable called error here now we need to inspect if the error is new and particularly we want to we care about when the error is not annealed because when the error is not near that that means there's something abnormal going on inside of the function that we just called and we want to handle the error being returned and in this case we'll just go ahead and print out the error and we're not going to do anything else here so if we go ahead and run this program we would be able to see that it is printing out the error which is printing out the message that we passed into the errors dot new function which is a built-in function in go now if you have watched my previous video on functions you know that in go we can return a function it can return a result of multiple types and that's what we're going to do here we will return the result of a type string and at the same time if there's anything abnormal going on inside of the function we will go ahead and return a non-nil error at the same time we will give it a parameter return error of a type boolean if the return error boolean is true then we go ahead and return an error while the result of a type string will be its zero value which is an empty string go ahead and cut this here and paste it here otherwise when the return error is a false we will go ahead and return some random results while the error being returned is new along with that let's go ahead and modify our instruction in the main function here assign the results to these two variable results and error also give it a parameter false if the error is not new go ahead and print out the error otherwise we will go ahead and print out the results run this program again as you can see it is now printing random result because we are passing in false if we go ahead and modify this to true run this program again we can see that it is a printing out error here now the built-in error type is all fine and dandy it does the job right we can pass in a string as a parameter so that we can print out the error message when we detect that error is not new here however what if you want to have a error type that has a little bit more information then we can go ahead and declare such a type we will call this error a special error for this special error we want to have error message still of a type string but at the same time we want to add a another field called error code which is going to be of a type integer now that we've created a special error type how can we actually use it now of course a very brute voice method would be replacing all the places that you have error type here replace it with the special error type obviously this is not what we want to do because it will take a lot of work depending on how many of these functions we have so that doesn't work but we can actually go ahead and hold on to the control key or command key if you are on a mac and click on this error type it will take us into the implementation of this error type and as you can see this area type is actually an interface meaning that it doesn't really matter what kind of newly created error types we have as long as the newly created error types have this function or method implemented then we'll be able to use that a newly created error type just as a error type so let's go ahead and do that close this one out and have this newly created type a special error to extend the error interface also if you don't you aren't that familiar with interfaces in go make sure to check out my other video where i talked about interfaces for now we will go ahead and implement the error interface special error here and the function error which will return a string as a result here we will go ahead and return however we want the errors message to look like at the end which will be as a dot error message plus space in between and then stir convert integer to a string error code so this will be how the error message will look like at the end of a special error now that we have the special error type of fully defined we can go ahead and use this type here give it a error message saying that it is a special error and i give it a error code we just do one two three to keep things as simple here if the return error is true then we will return a special error otherwise we will return nil so go back to the main function go ahead and run this program again as you can see it is a returning special error one two three instead of the original regular error message so this is how you can use the built-in errors library as well as how you can create your own error types in go if you found this video helpful make sure to leave this video a nice little thumbs up and make sure to subscribe to this channel so that we can become my goal and ninjas together see you next time you
Info
Channel: Golang Dojo
Views: 10,036
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, Golang Error Handling ULTIMATE Golang Basics Tutorial, golang error handling, error handling in go programming language, golang error handling tutorial, error handling in go explained, handling golang errors, golang error tutorial, error handling tutorial in golang, error in go
Id: rMakfEv672M
Channel Id: undefined
Length: 8min 27sec (507 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.