This Package Makes It SO Much Easier To Write Golang Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you one of my favorite tools to use in the go ecosystem and no it's not part of the standard library but it really is going to make your job as a go developer so much easier so the tool I'm talking about is called air by cosm track on GitHub it has about 11.7 000 Stars it should really have a lot more but really it's live reloading for go apps I'm going to show you a quick example of what exactly this solves if the name isn't apparent enough but simply saying it just sets in the motivation when I started developing websites in go and using gen framework it was a Pity that Jin lacked a live reloading function and this is actually pretty common across a bunch of different Frameworks I know the Arthur here is talking about gin but this is relevant in Chi and marks and a whole bunch of different go Frameworks even just the standard Library when you spin up an HTTP server there's no so hot live reloading so if you wanted to make a change and make it obvious throughout your server you literally have to exit out of your program make your change and then re-spin up your server and that's just a pain especially going to do some quick like logging or you've missed something or print statement you have a typo live reloading makes your life easier alright so I'm back on the GitHub page of air and it's a CLI tool such an easy install so here you can just see with via go install you have two methods prefer install with install.sh or via the go install camera if you have go 1.18 or higher and if we flip over to our editor you can see Go version we're using go 1.20.6 even though we should be using go 121 but then let's go ahead and install Air at the latest version and now we can see that it is working obviously we're getting an error here failed to build Eric's status because it's not find the correct directory of our application but we're going to build that right now okay so you can see here I have a brand new project there's absolutely nothing there's a go mod file and a main.gov file if we go into the package we're going to do package Main and we're going to do a few things and for this demonstration I'm going to use the chi framework I think it's a great framing to use for go for spinning up HTTP servers the syntax and logic and the way it's written is just beautiful so Link in the description below I took some code from Chi this is just basic HTTP code I mean I literally just ripped it from their documentation you can see here as you go run main.go it's not going to say anything but if you spin up and go back to our server you can see here that we get a little welcome screen here however let's say you want to refactor this or you want to add a new route to this well let's go ahead and do this add a new route that just responds with some Json that says this is a healthy route http.request I'm going to do is response uh body map of string message and then the message you're gonna say healthy route all throughout and then we're going to do response Json is going to be Json dot Marshall we're going to put in the response body we're going to do HTTP dot error I'm going to put W and then internal server error and then HTTP internal server error all we're going to do is do w dot right header http.status okay and then w dot right response Json so we add a new Handler and then we go back to our main routing function let's go add r dot get to tune a get method which will be slash test that's the route and we're going to pass in our health check Handler here okay cool so now we've added it but if we go back to our server and if we go to localhost 3000 slash test which is the name of the route we just added 404 page not found well that's pretty annoying because what we have to do instead is we actually have to tear down the server and interrupt to kill the server essentially and then again do go run main.go go back refresh and now we finally see the healthy route Json message but you can see how this isn't the best developer experience I mean this just adds so much time you're not iterating fast and if I just had to quickly change this message on the route then I'm gonna have to change it tear it down spin it back up and sometimes in this example it's fairly easy but if you have a big show that's a load a bunch of things and you have a bunch of app client initializations that's a pain in the ass so instead of going again we're gonna actually turn down our server I'm going to use air again and boom it already knows that our server is running because they found the main.go file and it created TMP folder for us if we go back just to check refresh everything's working as expected but instead of putting healthy Rel let's just say you are seeing this in real time reloading and you can see that it rebuilt my server and there you go you are seeing this in real time reloading without me having to actually intervene interrupt the server and spin it back up again and there's one more thing I want to talk about and that's actually really cool usage of air when it comes to integrating it with a Docker file Docker compose file very simple integration you can just put a command of air and it will automatically spin it up so you can copy all your go logic into a Docker file add an error command install it here with a run go install command and you will have air hot reloading your go code even through a Docker container so hope you all enjoyed this video let me know what you think about air is the tool that you think you'd use personally I love it I use it all the time it makes my life easier and this is kind of those videos that's going to be in a new suite where I try to find new tools in the go ecosystem built by incredible authors that can showcase to you guys and let me know is there a tool that you recommend to other go developers that you just can't live without and should I cover it in my next YouTube video but as always I gotta leave you guys with two things one what go to do you like and I want to know beyond standard Library this really cool go-to's Main about really smart people so which one do you enjoy and number two you gotta power it
Info
Channel: Melkey
Views: 25,400
Rating: undefined out of 5
Keywords: video sharing, video, sharing, computer science, software engineer, silicon valley, computer programming, coding, learn to code, machine learning, artificial intelligence, cloud, python, javascript, how to code, Data scientist, AI developer, Machine Learning, go, golang, go programming, go tutorial
Id: erdDM_LmChs
Channel Id: undefined
Length: 6min 17sec (377 seconds)
Published: Tue Oct 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.