Master dependency management and Best Practices in Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] well hello guys and welcome back to my channel so today what would be looking at more or less would be dependency management and go and what I can quickly do is to more or less check out a new Branch so we can already get started without wasting much of our time so get check out p and I provide the branch so I'm checking out to ecosystem okay so what we are going to do today is more or less bit in depth look at um go modules and then we look at um what it means also look at what's um go some means more or less and then we can talk about some tricks more less you could employ to manage your your dependencies as efficiently as possible and of course um because we don't have much to cover on this chapter I will also like to talk to you a little bit about some of the things you could also do in preparation of getting your code ready for production so we're going to talk about Linkin we're going to talk about um um of course formatting your code standard um practices accepted go standard practices I will share all of this with you so that of course if you have to collaborate to tomorrow with um some other maybe much more experienced Engineers you wouldn't look um like you're in newb more list so these are some of the things we look out in this chapter so I also think of course this is this is going to be quite um useful so let's Jump Right In so of course simply put the the go modules more or less are you could think of them as um a dependency management system that more or less makes it a lot easier for for you to handle your or manage your dependencies so you it makes actually a lot hassle free so you don't have to individually um take out Lookout to make sure that your in your your packages and of course their dependencies are up to date but of course it's this does it for you like um as seamlessly as possible um of course this is similar to what um you would have in other langu which is like say um rust where you have Caro for example or maybe with um of course node where you have like mpm or year as the case may be um so of course this makes it a lot easier that to have like reproducible builds you know you have semantic versioning you have dependency printing vendoring as well and of course minimal version selection so this are some of the few things you get out of the box just working with um go modules and quickly of course I'm guessing you're not new to go modules because you s us of course use the command every um most of the time so with go modules of course you can use this to initialize a new project where you already are familiar with the command go mod in it and then provide the name of the um of the um modu model is the that's you're trying to be model is a new application you're trying to start and and with of course you I'm also hoping that you already know that if you want to add new package you can easily do a go get for example or you can also quickly just write the package name into your go mod and simply do a go mod tidy so this is also not just going to not only going to install the packages but of course if there are are packages you already have your application that you are no longer um or that you no longer need these are also going to get cleaned up so if you look into the go mode file you would see that we have like direct dependencies and down here you ALS also select indirect dependencies so at there was never at any point where we had to to install B than Sonic but um this is already um a dependency of another package that we had to store so that's why you see most of them down here notated with um indirect because we don't actually directly need them but of course go mod helps us to manage this both direct and indirect dependencies that application have I am also familiar that maybe you might have I've also seen um in some cases where people use um go install and go get and you might be wondering what the difference is actually it's pretty straightforward if you're wondering um actually more or less what's the difference between like go install go get go mod tidy for example go Mod download maybe you've probably seen people use these different commands set in different scenarios are you still a little bit confused as to what they actually could be used so the main thing more or less would be that with go install you install a binary and not a package so with go install you need to install a binary not a package so but with um go get you actually need to download a package so you can think of this actually as a source file the source code of that um package that you would want to get when you use a goget actually downloads this um this package for you more or less so but with go mod tidy this actually like a quite handy quite handy command and what it does basically is it scans your project and updates the go mode file you know and of course in the prodject in the process it downloads all the pendencies that are mentioned in your go mod file and of course gets rid of the ones that are no longer required by your application that are still present in your go mod file you know and of course with go Mod download so this only download the dependencies um that you have defined in your go mod file but it doesn't modify it doesn't modify the file itself so the major difference between this and go mod tidy is that when you use a go Mod download and you have packages listed there just going to go ahead and download it but it's not going to do a cleanup for you um for example if you have packages that are still listed that you no longer require in your package so that's the difference there guys between go install go get go mod tidy and go Mod download again of course if this is confusing for you please you can rewind the video go back listen to it again and and try to understand what we are saying here of course the next command you might have also heard or seen people use would be go um mode vendor and you might be wondering what this means so in essence go modules actually supports vendoring and what this basically means is that it allows you to include the source code of all your dependencies directly inside your project so normally this when you install packages or binaries this would actually be be stored more or less at your goal path so if you check your um EMV file um I think the command is go EMV I think yes if you if you look at your go EMV file you can always come up here to see um if we can find it um go path so if you go to your go path more or less when you download or install packages it's actually started in here but yeah with with go mod vendor more or less you would like to um actually download the packages and then put them in your current project so if we did a go mod vendor orless is going to download The Source um The Source codes of all the dependencies we have um depending on the vendor so some could be from I don't know from Google so can be from which other other developers on there out them more or less and it's going to download all of this and put them in our current package that's the major difference with go mod vendor um there of course there might be scenarios of course where where this might prove useful um and of course if you also want to look into let's say the source code itself and you can this could also be one one one way you could go about it and of course if you would like to then build your package based off of this um downloaded packages that you have in your vent that youve gotten from the vendors this dependencies can actually do something like go build and now I can actually provide the mod now I would want to use the vendor because again like I told you when you run the when you run the initial command go mod vendor is going to create a folder called vendor and then in there it's going to put all those packages so if you want to now build your project from this um downloaded um Source codes that are stored in the vendor folder you could actually basically do something like this and then the compilation of your code will actually be based off of the content of this vendors um um package again the difference is of course in there you have the source code right there in your project not stored at your root at the root of your at your um the go M path but directly inside your package so if we did it's going of course start that directly inside our package in here so that's just the difference with this and of course maybe if you wanted to see how this could look like if we did a go mode vendor for example to actually see what happens um just so we also say that's um or actually let's see if that actually works and of course if it does we would also like to see what it actually does in ense so um the command of course is finally done and now like I told you if you look over here in the root of our project you have this folder called vendor and then if I look into it I would see a of these um hosts more or less so for example we have over here um um bu dance if we looked into let's say GitHub and then on GitHub you can see over here we have bu Dan and then here we would see all um all the packages of course the source code that's we actually got from by Dan and then apart from that we'll also see a lot of other um um hosts that we have over here actually so we have the gene of course we using the G GSS at some point of course I think this is also part of the Jing Gonic um framework that we're using so this is what we get over here and of course the other things um the other packages that we have down here and of course apart from GitHub we also have like gitlab G goang itself so this actually like of course the ones provided by goang um and then we also have like Google and and the rest of it so like we said this more or less downloads the source code into your um into your project and now we can actually do a go build and now I can point the mod I'll point it to use vendor and I can do something like this and can actually do something like this so um go build and mod provide the file vendor and of course we are now asking it to not download the packages but of course use the um the packages that we have locally inside the vends folder to build our our binaries for us and then I can do an output or I can actually not do it and then it's going to of course um save this as a name of our folder in this case um It's called Go Go lank guide so that's what we have out here g.exe and of course if we also provide an output path and maybe call this main.exe and build this in the end we would also get a man.exe and this would be our executable as you can see down here and we can of course run this let's see man the es and then of course how you see our server is up like we've always had it um so that was what I quickly wanted to show you of course um with regards to go modules and I'm just going to remove all of this stuff I don't need it in a more so remove go L guide and get rid of this well okay the next thing I would want to talk to you a little bit um would be about Flags so if you look over here for example in our main. go we have this version here which is more or less we currently Define it as a constant and it's currently set at V1 and we if you can remember we are piping this down to our API definitions so that's when we run our app go men. go um go run sorry men. go and when we run our server this way you would realize from the way we set it up of course you realize that all our end points are appended or prepended more or less with V1 so this is a way that we try to version our apis now what if we wanted to set this dynamically without having to hard code it this way because of course each time we run the server we're going to get V1 no matter what and if we want this to be a different version what it means is that we have to go into this our code here and then maybe change this to V2 and we might not really want to do this because this would not be the way to go if you ask me because we would want to have make our app as generic as possible so we can make modifications on the Fly we might want to deploy this and then maybe when we run the deployment pipeline for example would want to then change this from V1 to V2 or whatever we want it to be you know so one cool way we can actually do this would be to use um Linker flag or more or less the artifa as LD flag in go so to modify this on the flight it's actually pretty straightforward so we use the um the link of flags and I'm going to of course provide a link to the documentation if if you want to read further about this but um I'm also going to explain a little bit further on what this means but I'm just going to quickly show you so if wanted to build our our app one more time and then we would want to modify this version for example instead of con I can Define this as a variable so um we can modify it more or less and now I can try to build my app but now I use the LD flag so I provide the LD flag this way and then I need to provide a string I need to provide some more arguments more or less and I'm going to explain what this means um briefly and now of course um I need to provide more or less what I want to change here inside this um single quotes So I like to go inside of the main. go file and then the variable inside is called version and I can actually now send this to maybe V2 like this and then I now I would like to Output my binary I can call this m Doe executable more or less and now this is going to build our app and then what is actually going to do is more or less is um we use this x um flag more or less to come into our M.O file as you can see here men and then I'm targeting the version the one we have over here and then I'm changing it to V2 so now if we run our app one more time so the of course executable we have output for us um I need to run this way so if we run our app one more time what you will notice that is we don't get V1 anymore right now we get um V2 and then of course you would realize our good code also didn't change it's still V1 up here if we stopped the server and did a go run men go um we of course should still get our V1 so using this link of flags what happens is that we get to um more or less on the Fly change certain attributes um of our app and what we can actually do for example is I can maybe cop do this this way because I would like to Define more argument and maybe down here I can provide an argument and I can call this date it's going to be an empty string and maybe in here I can print out a statement I say fmt print line some like this and I say um app was built on something like this and I provide the date and what I can do again is also to use this LD flx to modify this um parameter more or less to actually use the correct time that this server was run so that's one so that's exactly one thing another thing we could do and all we have to do more or less is to use the same old command we used for the building so this one and then I can come in here and provide another argument this way of course within single quotes and this would be main. dates like this and now I can actually use um um shell scripts syntax more or less so I use my dollar sign within um parenthesis is like this I can use the date command from sh script because of course you know if I go to a new tab and I simply say date I get the current date something like this and then so if what I can do is if I run this command um this would build of course our app again and now we have to run it again one more time so we run our men. exe and then if we go up somewhere we would get our argument here app was built at so so time so this is um a really cool future you can already imagine so now let's talk a little bit about what this LD flag actually mean so these are actually called LD flag or Linker flag um because um more or less um they are the you could actually think of them um as so if if you are familiar with linking in go where you have um the linking program in go that basically of course links different pieces of our code together in the end to have a final binary um so this is more or less the go like within the goto chain more or less the link within the go-to chain that um we try to interact with at um when our binary is being linked and built in the build phase you can think of it of course because this is happening in the build phase so in the build phase what we try to do is to to more or less utilize this Flags to do perform certain modifications to our code while it is being linked and built into our final binary that's really basically what we're doing with this so with regards to that of course like we said this is happening during The Bu phase and to understand a little bit more actually what's actually going on under the hood and of course some of the other possibilities we have um because we actually have a lot of power um when it comes to what we would want to do during the build phase what we can actually do is I'll quickly stop the server and then we do a go help go help if I can type build and then what we can do is if we scroll up up here you can see um we have our LD flux up here and then it also tells you these arguments to pass on to each goto link invocation and of course besides LD flag if you have time you could actually more or less look through this you can already tell there are lots of arguments that are possible of course within um um when within the build phase or more when you're building your application and to get more insights with regards to like the arguments we use over here um when we we are like over here we had S and W and of course this capital x um what we can simply do is we simply say go to link and now we can see all of most of most of the um arguments um we used and even much more so here we have um s which of course as you can see here it's disable symbol table and then if we down here of course we have this to disem dwarf generation if we go up um if you scroll up of course you can see here we use also use capital x so you can see here it's a definition to add string value definitions so the form import path SL name and that was also what we did so the import path for us was the men. goo file but of course we don't need to provide men because again this is within the main package not because the file name is main but because this is within the main package and that's why we use Main and then the name in this case version and date and there are tons of other um arguments of course that could be passed and you also of course are invited to take your time and read this through you don't of course need to know all of this to work with go but the point is that to understand that some of these tools are there at your disposal so that if you for any reason run into a problem in the future while working with go and you don't know which direction to go it might also be that the solution to your problem is already um available within the goto chain and you probably just are not aware of it so the the a of this tutorial is of course to call your attention to some of these tools so you can take a look at at your own dispos um disposition and of course know that they are actually there for you to use so you can already tell if we if you if we have our application let's say um we would we have let's say we trade environment we have a production environment let's say we also have like a q environment maybe we also have an even a test environment I don't know and we develop let's say a new version of our API say version two and we would want to deploy this version to into the test environment so we can test if everything is as expected so of course you can already tell we do not need to change our source code we only need to add um commands like this in our pipeline could be if you're working say with GitHub actions or the gitlab um pipeline or git or whatever um code repository out there that we're using just so you know you have the option to actually B this into your pipelines and then you get to deploy this specific version into specific environments and that's really more or less what I wanted to show you over here and the last one of course we can talk about talk about here would be Linton before we go into Linton first I I want to do more less would be to format my code so I do a good fmt and I think I can provide this this way to get deep formatting no go fmt um so I can also use this fla s SW what um yeah of course I need to provide the path so of course this was correct I need to provide a period to show that I want um provide the path to show that of course this would be in the current directory where we are so you can see almost all our source files we are actually changed so but the point of course is that this would format your code according to accepted um um according to accepted standards within the go Community now of course maybe I can actually get rid of this vendor folder I don't need it anymore remove RF and vendor cannot what what is this cannot remove device resour busy um okay I would get rid of this letter that's totally fine so but what I would want to do more or less is to provide is to talk to you a little bit about Linkin so there are certain um agreed upon you could think of them as agreed upon um methods and ways of writing go code so ranging from the way you you name your variables to um the way you define functions the way you to more important things um like when you um when you repeat yourself or when you have duplicate copies of code and stuff like that so the point is that with LinkedIn you get um it's quite possible to more or less um sa through your code and make sure that it follows this defined and agreed procedure of writing code in go so um to do this of course um you would require the goal and C package it's actually called go and C like this and to download this I would leave l in the description so you um can visit the the homepage more or less to see how you can download basically it's pretty straight forward you could just C this and then install this you could Cur the the sh script and then run the sh script it's going to install this for you basically that's really the point the main thing of course would be to have this in your in your in path in your path so if I just run this commment go L go l c and I think it's called go l seeing like this yeah so this is already I have this in path so the main point of course is after you download this you need to have this added to your path so you able to use the the command and of course this doesn't work alone or mostly doesn't work alone it to require a yam file where you specify the Lenas because of course there are lots of lentas out there and go and seeing is more or less like um a linon engine you could think of it in that regard so and what it what happens is that if you you have the option of specifying the lintas you require and you also have the option of telling it to use every possible L out there but this is of course not recommended because you could have your code everything working quite well and it happens that somebody somewhere publishes a new lter is going to pull that new lter in and if this in any way doesn't conform with the the what you have already set up in your um Pro your project of course your your build is going to fail so I'm quickly going to provide is a go and cile we'll go through it a little bit and then we'll see what we have okay so I've going ahead to provide a go C file and if you look in here you can actually see that there are lots of things here but you don't need to um know what everything means for now um again like I said I'm going to provide a link to to the the page the homepage more or less of goang C I think this be at goang c. run and then they also have a lot of sta templates um that you can more or less pick up and put in your project um with um the the barest at least beest minimum requirements that your coding would need to satisfy or more or less comply with as the important things I would like you to see for example would be this L section the first thing I have here is disable or true so like I told you initially the the standard thing more less to do would be to basically disable all linters and after that under the enable block you can now provide all the L you need and they do different things so um I also don't even know all of them um more or less um of heart but of course um you have the option of looking into each of these um individual L are basically what they doing so this dup more or less make sure that you don't have duplicate blocks of code um in your in your in your project and then I think go count makes sure that if you have let's say you have um let's say we have somewhere in in our code here that we used I don't know some string that we defined the same string we use it at maybe 510 place so this is going to force us to define a constant with that string and then use the variable that we've defined so I think go critic more or less looks into like go bed practices things like um um let's say if you if you do like a heavy um just imagine for example if we had I'm just going to show you an example so imagine we have um let's say I'm going to call this say users something like this and this is of let's say this would be of type I don't know maybe I Define a user here user for example and then I give it a name which will be a string string and and an email which of course be a string and here let's say users are of type email sorry user and then I can maybe provide some users for example so if we do a if we look through um our users more or less so let's say we have over here user for example and we are loing to users and maybe um fmt the print line and then we are printing our user for example if we have something like this um oh sorry I I need to get rid of this so if we had something like this um if you do a follow Loop a follow Loop this way with a range over let's say an array of structs in this case user and what is going to happen is of course this is going to get copied more or less for the number of times we have um um let's say if we have if this array of users are let's say 10 in length so this is going to actually get copied 10 times um when we Loop over our our variable this way and so if you use this link for example it's going to critique this um um this method of looping three your variables um because again it's going to point it out to you okay this is the problem and it will advise you to more or less use either a reference to this variable or use indexing so this more or less what this does I'm going to get rid of this this anyways so coming back here of course you already know G fmt so this is going to of course take care of the formatting of your code and a lot of other so this misspell if for example over here we we did something like instead of was we have something like this this linter here is going to say Hey you made a mistake here that's actually wrong might be a little bit annoying but that's what it does again like I said in the end you decide which LNS you would want to use and the ones you want to get rid of and there are the list is actually way longer than what I have here I just have a much shorter list because I feel for me this is sufficient you know and of course under the Run section we have um so normally I think the default isit code is always one so this more or less is not required and of course concurrency is concurrency so the timeout would be the timeout of um running the Linkin like you have over here default is one minute and other of these other attributes we can set in here so here I'm also specifying the Go version and then for output I get to specify formats and so this there are lots we can do here and to be honest with you without even running the lintern I can already imagine there are there are probably lots and lots of ways that we are already violating like um the way we should have written our go code because if we did something like let's let's just go ahead and run the L so go go um what did we call it again go l c link and simply use the Run command and say run and then I'm going to expand this a little bit and give it a couple of seconds or probably minutes I don't know for it to complain for it to complete rather and then we take a look so the linting is done as you can look over here we have tons and tons um maybe not so much for now but um some problems that we actually need to fix and you also get the name of the L that actually pointed out the arrow so if you look here for example we have a string defined in this file you can also go to the file we have a string here defined and it's telling us that we've defined the string four times there are four occurrences of this string so it's advis not to make it a constant so we can use it other places without copying the string all over the place um like I told you initially and now you have over here for example it says user is heavy um it tells us the size of a user struct and not here to consider passing it by pointer so if you look at the function let's go there um so here is telling us of course that this the size of this user structure we have here is 852 bytes and it's advising us to pass this um as a pointer more or less like this and what's is oh sorry crazy of course this should be this way and then we have to find where this is used to fix the error so here I can maybe remove this like this um and we fixed this so this is also something similar is asking here to consider passing this by is also something similar and then if you look over here again we have um some lot more recommendations we also have the same user stuff here we actually have some we are shadowing where an import is shadowing a variable that we have locally um so if you look here we have a variable defined as DB but I think if you go up we'll also have an import called DB so this is of course really wrong I shouldn't be at all and we actually have it in like three places and yeah we have a lot more things we have to look at so I think we are using a weak random generator as we can see over here and it's making a recommendation of what we could use instead and and and other stuff like that we can use that so we also have things like naming conventions we have underscores the names which shouldn't be because in go we we are more or less advised to use cam case for variable names and and stuff like that and to also show you if we come here and say um the user for example and we put something like this and we spell this wrongly if we run our l again the spell check linta will catch this and tell us oh we made a spelling mistake somewhere um let's quickly look to see if we can find it um hopefully we can find this um I don't think we can so us Sr e so we don't have do we even have the linta in the first place so let me take a look again um let's look at our lters um go misspell so I think of course we have a list here but once we fix because in the end these are actually not all the only problems we have that's also what I can tell you from my experience so if you look at this list you might think it's actually a small list but the point is you don't get all the outputs I think there should be some form of settings you can put here in this file to um dump everything on the console but other than that by default you don't get all you don't get the output of all the possible errors so what happens is when you the more if you fix the problems and run the ler again it will show you um it will throw out more errors basically so but the point of course is to um make sure that you follow the defined and agreed upon go style gout um more or less and that's that's really the point of um using the lters um and anyways that would be all actually that's that would be all I wanted to show you in this video I'm really hoping this is all this proofs helpful for you again remember what we Tred to cover in this tutorial so we looked a little bit at dependency management and goal l a little bit at um different ways of adding packages in your application and managing them so we had to go get go install go mod tidy go mode download and um we again talked about about talked again um about Linton anyways that will be all for now I I hope you've learned something in this tutorial we are going to again continue like we agreed upon and we are moving actually at a really nice fast pace and I'm okay with that um if you look over here we actually have um more or less four more chapters and then we'll be done with our tutorial of course that would not be the end of our Goen Co um Journey but of course um just like we agreed upon in the beginning the goal of this tutorial will be to give you like a reference um if you are starting as let's say as a backend developer working with go or if you're already are working with go and you would want to check out and see um how T does things this is of course should I I assume it would prove useful to you anyways and that would be all for go on now guys and yeah please go ahead and subscribe and and and of course um hit the notification Bell because I try to provide or more or less upload videos as as um as frequent as possible to always keep you um learning more or less so go ahead and subscribe um and we'll see each other next time guys have a lovely evening and bye-bye [Music] n
Info
Channel: Easy Dev For All
Views: 51
Rating: undefined out of 5
Keywords:
Id: cwlFTr7bGCA
Channel Id: undefined
Length: 40min 11sec (2411 seconds)
Published: Fri Jun 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.