Is Fiber the best Go web framework? Better than Gin?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign what's up YouTube Welcome Back to another edition of coding with Robbie I'm your host Robbie and in this video I want to talk about fiber which is a web framework for golang so for the past couple years I've pretty much only been using gin which is another framework and for no good reason I kind of just looked for the most popular framework out there and gin by far has the most GitHub stars and forks and users and everything so okay everyone uses Jan I'll use gin and I like Jin Jin it's great it does everything I need and uh yeah I never really branched out because of that I just started it and I liked it and had no issues with it but yeah the other day I was on uh I don't know why but I was reading about.net and uh I ended up on this y2s.net page and I was kind of just scrolling through reading about it and I got to this performance section right here where they're saying oh.net is the most performant popular framework out there which is debatable but they linked to this Tech and power website which I never heard of so I clicked it and yeah basically this company just benchmarks almost every web framework out there so the first thing I noticed is Microsoft links to the plain text test instead of the composite scores which is a little fishy but I switched over to this tab and I'm like oh I wonder where Jin is on here so I kind of scrolled through I'm like oh where's Jin Jin Jin Jin and I found it right here number 63 and it's got some good performance numbers I'm like oh it's awesome and then I'm like oh where's the rails because I used to do a lot of rails and rails is at the bottom number 131 and it's got four percent and uh yeah it's down here with uh larville and Django so then I'm like oh I wonder what's um golang but above gin so I went back to Gin I started scrolling up and uh yeah number 22 right here I found fiber and I've heard of fiber before but I've never really researched it or tried it or anything so I'm like oh wow it's quite a bit um higher up maybe I should try fiber so I went to their website and their website's beautiful it's got a nice design and they show some examples and everything looks easy to use and not only is it easy to use it almost looks identical to a gin just slightly nicer syntax okay that's nice and I'm just kind of looking through yeah I know it doesn't always matter but a lot of times the nicer the website the nicer the product so I go back to Gin and it's just like night and day as far as their website goes and uh yeah I'm like okay that's one plus for fiber and then I went to their docs page and they got this awesome docs page with like little emojis and it's just all well organized well written and pretty much fully documented so okay that's another plus and I went back to Gin and with Jim you know really to learn about gin you kind of have to go to examples and just look at example code it's not really well written like I said the detail information which I've never checked out and links to a GitHub comment so that's um a minus for Jen I just started looking through the fiber documentation and it does everything I would need from a web framework so I can create an app object it's got the context where you can like get params and do different stuff with it it's got middleware for things like Authentication and it can do cores it's got pretty much all kinds of middleware that you can use um the routing looks awesome uh they got some grouping stuff which I haven't looked at too much and then I went to templates and they support a ton of different templating engines which I'm pretty sure Jim just does HTML so that's another Plus got some error handling which is a lot simpler than engine and uh I haven't explored validation too much but that might be something cool to check out and uh I'm not sure what Hooks is but yeah basically it looks awesome and it takes all the boxes for me so I'm gonna play around with it in this video kind of show you guys the basic features and um yeah we're going to do that in a second but before we do make sure to like subscribe leave a comment let me know you're there and uh yeah let's get into it all right let's give her a whirl so I'm just on the welcome page right here and they have the command to install it right here so I'm going to copy that let's go to terminal I'm going to create a new folder called fiber test and then I'll CD into that folder and run go mod in it and then we're ready to install the module so it's paste in the command and that's going to download everything and uh let's create a main.go and then open it up in vs code now let's go back to the docs and if you scroll down a little bit they have a Hello World example right here so let's copy this and let's just paste it within our main.go and it looks like we're importing the package and then we're creating a fiber app and then we're adding some routing and then we're starting it on Port 3000. so let's run this file and make sure it's working and I'm using something called compile Daemon this is just a package that basically watches for file changes and then automatically rebuilds and restarts it if you want to look into that here's the GitHub I'm not going to explain it in this video but yeah if you don't want to use this you can just do go run main.go but yeah we should be running on Port 3000 so let's go there and see if we get it and we get hello world so let's keep scrolling down and uh here's how you can serve static files so you just add a line like this let's copy it so I'll add a new section here called configure app paste that in and we're saying hey let's serve uh any assets within the public folder and serve them under the slash route so let's create that folder public within there I'm going to create a second folder called assets and then I'll create a file style.css let's just add some CSS in here that's good enough for me and let's see if we can access this file now so we'll go back here and it should be under slash assets slash style.css and there we go we get it so uh let's add some templating now so if you if we go to the templating section down here supports a ton of different engines uh the default one is just HTML I'm just going to be using that and yeah they click on with that we have to install this additional package right here so let's grab that I'll go back to terminal and we're on go get and paste it in and I just downloaded it I'm gonna start my server again and then uh yeah we gotta load the templates like this so let's go back to our main we'll go to the top like they do the templates and it says hey they're going to be within the views folder I'm going to tell mine to look for DOT tmpo files which is kind of the standard for go templates and then we have to connect this to our app and to do that you just add some additional config so we can just copy this right here and paste it within new so now we're saying hey fiber use the this engine right here which is the HTML engine so let's create a views folder and I'll create an index.h index.tmpo I'll just put some boilerplate and uh let's link up that style sheet to make sure it works so link Rel is style sheet um href is slash asset slash style.css there we go and then let's just do an H1 we'll just put hello save that let's go back here and uh yeah so to render a template it's just C dot render and you give it the template name and then you can pass it some data so let's copy this and instead of just sending hello world we'll render our template and uh yeah let's see if that works we'll go back here and I refresh and now I get the HTML page and we can see our HTML right here so let's try the data passing stuff let's make it uh let's give it a name and I'll go Robbie hit save and then let's go to our template and we'll add dot name see if that works refresh and get hello Robbie so that's working good all right what else can we do what else can we do um let's try to get some data from a form so let's go back here and let's create a form and for Action it will also be on just the root path but for method I'm going to do Post and then we'll give this an input with name message and uh let's do a submit button so button whoops type is equal to submit and we'll just click Send message inside there so let's go back and make sure our forms are entering it is and now how can we get that data off the page or off the request uh it's probably in context so let's see maybe body Returns the raw request body okay we want this body parser thing right here so it looks like you just give it a struct or you want to store the data so let's try this out let's go back here and uh we have to add that post route first so let's go app.post also be the root path and then the second argument is a function that gets the context which is Star Fiber dot CTX and then it returns in here and opens up and then uh let's just have it render the same template and then up top here we're going to get the data so let's create a variable body and it's going to be a struct which has what are we sending we're sending a message of type string and then right here let's say hey let's store that in our body variable and then if we don't get an error message it should be working so let's go back and I'll refresh and post and I don't get an error message so let's uh let's pass the message to the template let's go message it's going to be body Dot message and then we'll render that on the page if it exists so let's go here I throw it's weird today so let's go if Dot message and then end and then if there is a message let's just render it in a P tag Dot message let's see if that works I'll save it go back um let's cancel that go back home and let's say a message hit send and it renders it so it's working good that's how you can get data off the page um let's see what else can we do how do we do middleware uh how do we create our own so for your own middleware it's a c routing slash middleware and looks like here's how you do it so let's copy this and uh let's create a new section up here called middleware and then uh you can set some headers and stuff like this it looks like but uh for us let's just log something with fmt.printline I'm in middleware and then you just do uh C dot next to carry on let's see if that works so let's go back home I'll go to my terminal and it looks like it printed it right there so that's cool and then if we wanted to attach that just to one route how you could do it is just take it out of this app.used and let's just assign this to a variable called m and then uh we could hook it up to a route if we want it in between the path and the function or the Handler so let's put it right there and uh let's just restart our server and I uh render the index path and uh where am I at go back to terminal I get the middleware right there and then if I post we shouldn't get another one so I post and then go back and yeah we don't get it so that's working good um what else can we do grouping so you could organize uh your routes and groups let's try that out let's copy this so let's create a group I'll call my group pages and then uh let's attach our middleware to it [Applause] so let's delete it from here since we're attaching it at the group level now and then we'll call this pages and then instead of putting app right here we go pages and let's just add a second one to make sure it's working Pages slash two all right let's try that out so now if I go back here it should be slash pages and get the page and there should be a Pages slash two also there we go and it should be logging our middleware I only got one why is that let's see two two two two two on Windows if I scroll it doesn't show all the logs so there we go it is working so let's see what else we can do so we did routing what else is there um it's Constance there's a bunch of constants you can use for error messages if you want um let's try error handling so pretty much you just return the error and it's going to do an error page so let's try that out let's go uh right here okay let's just return an error there's an error.new I think for errors.now how do you write an error let's see all right let's try those constants actually so say we want to do a 503 error we could go fiber Dot error service unavailable and uh let's try that out we're on two is that what I did it on no I did it on the uh Slash Pages route there we go get service unavailable you can do whatever kind of error message you want here so say we want forbidden we could do air forbidden and we get forbidden and uh see what else we got in here and uh if you don't want to crash your whole app you can add this recovery middleware and uh if we wanted to make a custom [Music] air we can do it like this let's try that so it's going to be an unavailable error and we'll go now we'll just leave it on vacation let's see if that works there we go that's cool um let's go on routing a little deeper let's try to add some parameters to it so let's uh do name so let's go right here let's just make it slash name slash and then we'll take a name and we'll take an age also and then how do we get those off the URL let's see pretty good seat up params okay let's try that let's go name is equal to C Dot params and we want to get name off the URL and we want to get age off the URL and then let's pass it to our template so instead of hard coding Robbie I'm going to put name and we'll add age also and uh let's try this out well first we got rendered on the page we already got name and let's go age my age is going to be dot age and the other path doesn't have age so let's uh wrap it in an if all right clean that up a little bit so we got hello.name which should come from the URL and if there is an H um it'll get it from the URL also so let's go back here and slash Pages slash names slash Robbie slash h slash 32 let's see if that works and I get Robbie h32 if I change it to Jeff H25 I get Jess age 325 because I left the three there there we go so uh one other thing I wanted to check out is Json stuff so let's search for that see Json and now let's see it looks like instead of C dot render just the C dot Json and you can just pass it destruct or whatever you want so let's go up to our index path which is right here and instead of this error let's go return C dot Json and then let's give it a struct so let's get a struct that's going to have a name string and an eight whoops and an H int and then let's give it the data down here let's go name is Robbie age is 32. let's save that and see if that works let's go to slash pages and there we go I'm getting some Json so that's nice and easy and there's some other stuff you can explore on your own uh you got a validation package that lets you validate data coming in I'll save that for another video and then they got some hooks where you can hook into different uh I guess life cycle methods of your app and then yeah there's all that middleware we saw with like cores and csrf and there's a bunch of first party middleware you can check out but uh yeah I would say fiber is pretty awesome I'm definitely going to be using this for my personal projects in the future and really there's nothing tying me to Gin this pretty much does all the same stuff and I think their syntax is nicer it's better documented and uh it's more performant so it's a win-win-win so I hope you enjoyed this video make sure to like subscribe leave a comment and I'll see you in the next one bye
Info
Channel: Coding with Robby
Views: 37,610
Rating: undefined out of 5
Keywords:
Id: 10miByMOGfY
Channel Id: undefined
Length: 21min 7sec (1267 seconds)
Published: Tue Oct 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.