Introduction to Go Fiber (and why you should NOT use it)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
introduction to hugo fiber today we'll take a look at what a gold fiber is how we can get started by writing some basic rest and a point and then we're going to circle back into why you should not use a gold fiber even though it's gotten a lot of attention recently and what you should do instead first let's take a look at the dogs fiber is an express inspired web framework when in ego so it's especially perfect for javascript developers are coming from a node.js background or express background it is a built on top of fast http we will talk about flash ap a little bit more in detail in just a minute here but for now let's start coding with go fiber [Music] first create a folder and open that folder with your preferred ide next let's go ahead and initialize your project with echo modules and that should give us a go mod file as you can see here if you still don't know how to use the go modules please let me know in the comment sections if there's another view i'll make an extensive tutorial on gohan modules because the go modules is essential when working with go project now we need to import a gold fiber into our project for that let's go back to the documentation this is the line of instruction for the gold fiber installation so let's copy that and paste hit enter now we've successfully added a go fiber into our go project and if we click on the go mod file here it should get updated to say that we are now requiring this dependency into our go project it will say indirect because we haven't started using fiber so let's go ahead and create our application and start using a fiber as you can see i have the application listening at the port 80 so that we don't have to specify the port number when we are typing in the url to make the request next let's go ahead and kickstart our server and make an actual request if you are on the windows click on allow access as you can see we are running our server on a fiber right now let's go ahead and use a postman to make a request add a request call localhost click this ascender button as you can see eliza we are getting some sort of response from the fiber server that we are running now let's go ahead and make our fiber server return at least hello world i said go back into our editor type in hello world restart our server go back to postman hit the center again as you can see now we are getting hello world now that we have the basic hello world app is set up let's go ahead and start writing our very first get a ninja endpoint let's go back into our code editor this will be the path and the function which we will create make sure the parameter is the context now we need to create our first endangered object to return a ninja will have a name and a weapon now that we have our ninja objects are created and we can go ahead and return this object as a json response the status will be okay and the json response will be coming from the wireless and ninja object also make sure to return an error as the return type now we can go ahead and restart our server go to postman and create our first get a ninja request add a request name it get ninja logo host slash ninja go ahead and click on this ascend button as you can see we are getting our ninja object return as a json object right here now that we have a get a ninja endpoint and let's create a create a ninja endpoint as well let's say go back into our code editor here a create ninja endpoint would be a post endpoint it will have the context as the parameter here as well returning error the very first thing we need to do here is getting the body of the request and then transforming that into a ninja object if the error is not new we will go ahead and return the error before returning the area though we will go ahead and set the contacts here and then go ahead and return the error like this make sure you are returning the actual error instead of this cn string object because it's always going to be new here so if we return this one instead then we will always return a new which is not an error so we should return the error right here and in the case that there isn't any error we can go ahead and create the ninja now we're going to create some very naive database just imagine that this is the ninja in a database i use a global variable that lives across at the server endpoints now we're going to go ahead and we're telling that there's a ninja as a json object response will be okay and go ahead and restart this server go to postman and now create this create ninja request add a request name it create ninja this will be a poster request in the body here we can click on raw and also make it a json go ahead and send this request as you can see we've successfully created the ninja wallace and with weapon ninja star now that we have a create ninja endpoint so we can go back to our again ninja endpoint to return a ninja that's been created by this create ninja request instead of just a dummy ninja as you can see right here this is a dummy nature that we are returning let's say go ahead and return this ninja that's living in this database at this very naive database right here and let's go ahead and restart our server go back into postman now when we are doing a get ninja right here we used to get wallace ninja star we're gonna get nothing now but if we use the create ninja object and then call the get ninja request again we will be able to get a ninja that's created with this create ninja endpoint if we go back into the main function here as you can see we have at least the two ninja endpoints right here one is at the get a ninja endpoint the other one is the post create ninja endpoint wouldn't it be great if we can group these into endpoints together and i call them as the same application since they have the same path and that's what we're going to do right now now we're going to go ahead and restore to our server and see if the end points are still working as you can see they are working perfectly fine the next thing i want to talk about is middleware which is extremely common when you are developing apis a middleware is a function that's a chain with the http request a cycle so that you don't as a developer you don't have to specify these functions every single time that you are making a request or handling a request for example if you want the handler to automatically log the request information then you can use a logger for example for that purpose another example would be a request id so if you wanted to have a request id for every single request then you can use the built-in inside of fiber the built-in request idea middleware as well those two are going to be the two types of middleware we are going to be talking about the fiber framework makes adding her middleware extremely simple all you need to do is call the user method and then pass in the logger dot new like this and if we go to the dependency as you can see we are importing the logger from the fiber framework right here let's go ahead and restart the server go to postman and try to send the same request send a create request and then send a name gel request and also we are going to send a hello world request as well now if we go back into the terminal as you can see we are getting a post requested to the ninja endpoint and we are getting a get request to the ninja endpoint as a login information of these requests however we are not getting the request information for the hello world because this hello world endpoint is set up before we call the use a method to set up the logger so make sure that you are calling the user method before you are setting up any endpoints that you want to have the effect of the middleware that you are setting up also if you want to have a request id for every single request that comes to the server we can go ahead and add another middleware request id new here let's go ahead and restart the server again call the create ninja method now if we go to header we will be able to see that there is a request uuid same thing for get ninja endpoint as well as you can see here and the hollow method would not have the same thing as you can see there isn't a request id for the hello world endpoint and again that is because we are setting in the middleware after we set up the hollow wall endpoint so this is the introduction to her gold fiber so let's now talk about why you may not want to use it even though it seems also amazing so far first of all i want to point out the obvious i've actually written the exact same apis but it's using a native echo instead of fiber and the apis that i've written with the native go is just so much more convoluted there's so much more code that i have to write so i i definitely want to point out that there's definitely use cases to use a fiber and to use frameworks in in general when you are using a web framework like a fiber you can set up your project so much more quickly you can develop with much less boilerplate and you can also have some reinforcement for security as well for example you don't necessarily want to set up your own section and management all the time when you are developing apis however there are also arguments against using a web framework first if there are bugs in the framework itself those are the books that you can't necessarily fix right away because the framework is maintained by someone that's older than yourself than your team second you may run into compatibility issues in the future if this is a long-term project that you are working on thirdly there might also be overhead that you may not want to add into your assistant project it might be overhead in both the size and the speed of your weber project these arguments are particularly applicable for gold developers since there is a http package in the goes standard library that can allow us to develop web applications relatively easily without third third-party however frameworks on top of that remember in the beginning of this video we mentioned that go fiber is built on top of a fast http even though fast http is a great http engine it isn't perfect and in particular it does not support http 2. http 2 was a standard introduced six years ago in a 2015 so you might run into quite a bit of compatibility issues so does this mean you should just forget about gold fiber all together as your weapon framework of choice not exactly first of all these issues may not even be a big deal depending on the specific projects that you're working on especially if you are just working on a small personal side project second the go fiber team is working hard actively working hard on these issues so maybe just hold tight and the light is on the other end of the tunnel for the time being make sure to subscribe to the channel perhaps i have recommendations for other weber frameworks other than fiber and also make sure to get your free here like sheet at the glengojo.com t-shirt and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 3,135
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go programming, golang for beginners, golang web app tutorial, golang web apps, web apps in go, web app golang, golang web development, golang web dev, golang web development tutorial, go fiber, introduction to go fiber, Introduction to Go Fiber (and why you should NOT use it), go fiber in web application, go fiber tutorial, go fiber project, golang fiber, fiber web framework, fiber
Id: INt68SlX-LE
Channel Id: undefined
Length: 17min 39sec (1059 seconds)
Published: Fri Dec 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.