What Is THE BEST Web Framework In Golang? Why?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I recently had a discussion on my Discord Channel which is by the way for everybody free to join there's over 3000 people in there 500 people online continuously sharing good stuff about programming in general also mindset and all that stuff and a very good memes also so join that if you want uh consider subscribing to my channel only if you like the videos that I'm providing to you give me a thumbs up and leave some questions in the comments yes for the people that don't want to level up check my patreon and check the full-time code of course 50 off so uh yeah so like I said there was a discussion on the on the Discord like yo new people coming in and they ask what framework should I use if I'm starting out with goaling and then people say yo you don't need a framework because the standard library is just so good you can build everything yourself with only the standard library now some people say yeah but you can you better use good alarm mix or something because the message of um the standard library is complete dog let us be honest something they actually should already improved but yeah this is what it is so people say yeah you can use Chi it's also it has a little bit of more convenient helper fictions and it's also compatible with the standard library and and I always say just use Fiber or um uh Echo right or maybe gen or something you know what I mean because this there are basically two types of Frameworks in my opinion in Googling that are basically the ones that have the standard Library HTTP Handler function signature and the ones that basically use something like a context with an error return and I'm a big uh believer and I'm a big fan of the uh the ladder right which is basically the context with the error return I think returning error switch or HTTP handlers is such a good way of cleaning up all your handlers making sure they're very tidy and clean and also you can basically centralize your error handling so we're going to take a closer look right now so basically I have this thing set up Pete we have a handle function here which is the classical uh Handler HTTP Handler func Shenanigans usage and we have handle get usage um function here right and if you're going to check um the HTTP Handler func right it's just an HTTP response writer and and pointed to an HTTP request and that's it so basically let's make a functioning function Funk uh get user right now we're going to return heat a user actually led to a pointer or something and an Eros right and we are going to return uh nil nil heat just for the sake of having the compiler happy might satisfy this thing type usage is going to be a structure with nothing in it so everybody's happy right so if you want to get our user heat what we're going to do is we're going to say use it it's going to be get usage right and then we're going to say if the adult is for example could not find the usage database not reachable I don't know what added it is we need to handle that right here so how are you going to do that well you're going to do something like w right headers right gonna write a status HTTP status could be let's say a not found or something in this case right and then we need to do something like a Json new encoders which is going to be W and then we're going to encode uh some errors right you could do for example this is all uh quick quickly scaffold code right we're going to say map string maybe any or something uh and I'm gonna say that the error here is going to be for example Ash Eros something like that right and now we need to return here right because otherwise we're going to double right on headers to uh the browser and you're going to have some other like uh we just wrote the header two times that multiple times that's no good right so we need to return right and in this case we're going to say uh okay we got to use it for example handle actually though we're gonna return the UCD so we're gonna do again we're gonna copy this right header right we're going to say HTTP status okay and then we're gonna do the same thing here just copy this return save for formatting we're gonna say encode uh the user right just like that and call it a day actually you should also handle this error coming from new encoders from encode here right it's returning an errors ah questionable but hey that's up to you right so this is basically how you would do a very simplified version of getting a user from your API and return some stuff to the screen uh you can already see that this line each time you have air not nil right you could for example you have multiple admode Nils in your code right you can see that it's going to be very nasty you know what I mean so that's why I like guys that's why I like these Frameworks like Echo fiber or gin my personal best is maybe fiber Echo one of these two right because they have actually an other way of handling that stuff right so for example I already installed Echo heat so I can do uh for example e is going to be Echo new I'm going to save this thing we need to put uh version 4 here yes just like that and I'm gonna say e gets for example are you going to do the same thing you're gonna say slash usage and we're gonna say handle Echo get usage if I can do something correct something like that so we're gonna do this copy this uh let's paste it in here Funk paste this context C or CTX whatever you want actually I like to you see and I use CTX for the context dot context from Google line itself so we're going to say here um it's going to be an echo context right echoed context and I'm going to return an error right something like that so what we're going to do now is we're going to do the same thing so we're going to say usage at is going to be get usage right and then if there is an arrow hit right then we're going to basically just return the arrows right and in this case we're going to return C Json http [Music] status okay right and then they use it just like that look at this so this is much more cleaner in my opinion right and you can now centralize your error handling and also return custom handlers the cost of Errors right for example uh because this is going to return who the knows it could be an error from your database it could be an error coming from this function here which in this case yeah we don't know there is there is nothing but you can you know what I mean it could be an error and coming from a third-party Library so you don't know it's very dangerous to basically just dump out errors to your user because if it's you don't want to leak this secure information like uh there is no SQL you don't want to leak any SQL information from your database that the user don't really need to know right so what you could do is for example you can make an analyt type API error for example which is going to be a structure right a structure and we're going to say for example a status right status going to be an integer and I'm going to say maybe an Eros or a message maybe or maybe that if um actually to be honest I'm going to do this I'm going to make it a string right and now we need to implement each for example API Eros uh I'm gonna call it e right and we're going to say error function which is going to return a string we could do something like return e add or something like that um yeah let's do message because otherwise we have conflicting things right we cannot rename this error function heat a function name because that's going to be implementing the error interface which is just going to return can I do this you see what I mean this arrow is an interface and it just returns a string right so everything that returns this uh implements this interface will be compatible to return as an errors so why is this handy why are we doing this well we could do something like this right um we could do something like API errors right and then we could do for example the status very important right because we need the status code so we can return the right status quote in our centralized error Handler for example HTTP um what was it status not found or something right just like that and then we're going to say the message is going to be I don't know use it not found for something who cares right something like that right so you can see that right now we have this thing here so what we could do uh not quite sure how it works it's going to be e dot error handlers HTTP header Handler is going to be something I don't know what it is I'm gonna quickly check this function signature here let's see Bang paste that in it's going to be a very long new to you uh something like that I'm gonna say if API Ash OK is going to be errors API errors something like that if I'm just trying to quickly do this guys so it's not going to be perfectly like prediction ready stuff but it's to give you guys some ideas here very quickly and the video is getting too long so uh we're gonna basically assert if this error is going to be an API error then we can actually do something like um see why this is not working what's going on each uh oh yeah it needs to be an echo an echo context to each copy pasting yeah you could do c Json um API Edge status and then we could do something like what we did with already write something like an Annie map sing any and then return uh uh Eros or a message whatever you guys want to call it's going to be API dot um what's going on each I'm confused how's it going on message message that is this why is it not working map showing any errors yeah yeah please please please please okay something like this right uh we could return heat and then if it's not an API addos then we can handle that like like just normally uh maybe we could do something like this one um it's gonna be an internal server something we cannot handle uh we didn't handle ourselves we're gonna do an HTTP internal server Eros right and then we're gonna do um some custom message like uh internal server error just pinging figuring things out on the fly right so this is going to be your centralized error Handler right and um yeah like I said you can make multiple custom errors if you want and if you really want to take it a little bit further what you could do is instead of this nasty thing here you can actually make a function for example um usage not found Eros or not found at all basically something like that I don't know it's going to be a function that's going to return an API error for example right we could do return you can do so many stuff guys you can you can make it as you want right you could say the status is going to be HTTP not fun I'm going to be a very quick hit and then we're going to say the message is going to be uh not found or something whatever guys you need to be um uh create a feed and I'm going to say return not found Eros right something like that you see um and actually what you could do you can make it even even better for example not found Eros uh an S is going to be a string uh wait wait wait it's gonna be the uh usage for example like that right and you can give it here um I don't know R from resource I don't know string and then you could do something like um Art Plus not found so it's going to be usage not found you know what I mean hey just be creative try things out you know what I mean it's it's it's all coming uh sucking things out of my thumb right that's what I do best um so that's it right so I think it's a little bit more um flexible because if you do it like this it's just I don't know um it could clutter up I did it before and it could clutter up your your handlers it could make it a very nasty and we don't like that on the other side uh you can also if you don't want to use echo or some other freeway that actually has a context add or finishing signature what I do back in the days is you could wrap your HTTP Handler functions with your own um decorated function that actually is going to return an error right in this case you don't need to use any of these Frameworks but you decorate your Handler yourself and I think I already have a video about that right so that's basically it in my opinion there is no such thing as the best framework or the worst framework it's just on your personal preface preferences how do you want to handle your uh your errors how big of a dependency do you want to get into your project it's like I said it's all whatever you like I'm just sharing here some of my opinions uh take what you like and drop what you don't like and for sure share your opinions here in the comments what do you think about these Frameworks like an echo or a fiber that has this context error function signature instead of the standard library that basically do not um returns errors do not let you return errors unless you're gonna decorate it yourself let me know that and I'm looking forward to see you in the next video live stream or something else cheers stay strong love you all
Info
Channel: Anthony GG
Views: 39,481
Rating: undefined out of 5
Keywords: golang, web frameworks, golang web framework, golang echo tutorial, golang gin, golang chi, gorilla mux golang, golang tutorial, go lang, golang for beginners, learn golang, go programming, web developer, golang frameworks, web developing, developer, backend web framework in go, software engineering, go programming language, go language, programming, go tutorial, golang web app, software developer, golang tutorials, golang programming, go programming tutorial
Id: oRZ6cQ0vmWg
Channel Id: undefined
Length: 14min 39sec (879 seconds)
Published: Wed Sep 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.