Master Go Modules in under 13 minutes - Go / Golang Module Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how y'all doing now i'm back here again and today it's time to finally learn how to keep those pesky third-party dependencies you are inevitably going to be using under control um quick aside okay why'd you bump my camera honey uh excited about dependency management and go it used to be a mess you'll notice that go has a lot of nice things sort of built into the tool chain um formatting testing build tools uh installation tools all this nice stuff but up until about three versions ago it didn't have any dependency management tools built into it so that meant that the community had to create their own um the problem is that there was not a like one globally used solution i believe there was three main ones used one was called depth one was called i think it was go depth and that was called like go vendor this created some problems where depending on what you if you went from project to project you may have one team use dep another team used govender blah blah blah do you see where it might get a little confusing uh but now luckily the go tool chain now has its own built-in way of handling dependencies called modules and this i'm going to be showing you how to what to use today in order to handle your dependencies so i have a blank project at blank directory here there's nothing in here whatsoever we check it is empty so we want to set this up to be able to use go modules to handles dependencies so how we do that well there is a command you can run with go called mod which is what basically what handles all the module stuff and we do go mod and then to start it you do init as you see here from my history you have to give it a name to initialize with typically you pass in the url like the the repository you're going to be storing this package in if it does if you're not really going to put this on a repository it doesn't really matter um so i'm going to call i'm going to let this auto complete so if i was going to put this code in a repository this is where i would put it which is on my my gitlab account under this name here called getlang modules if i run this you'll see it's created a new uh go.mod file and also i have some like um uh plug-in on my terminal that's detected that hey this is a go project and this is what version of go choosing so this has created um a file here called go mod and if we look at it you will see right now there's only two things in it there's the name of the module you're working in which is what we just passed into go mod in it and then what version of go this module is using which as of right now is 1.14 um so how do we actually manage dependencies here well let's just create a small little file here right so you notice there's already some uh bowler plate thrown in here for my little my um plugin so we're gonna get rid of this and oops go back up here so i'm actually going to just do something that imports in some random dependency i recently had to start doing some things with a package to interact with discord so let's this is pull in the um the discord why not um you'll see here it says it's called discord go and this is what you do to use it you do this right here so i'm gonna copy this because we already have an import statement and we're gonna pop back over here and let's see i'm gonna wrap this in good old braces like this uh get down here and we're gonna pop you in oops oh i made go back over here copy this again please give me this i don't see put this in yeah oh what's up i'll just do it man oh jesus github.com hug github how about nice dot com slash vw marin slash discord go so if we go look at this right quick here's just a simple little snippet to do with um so this is actually what i'm going to do i'm going to copy this i should put that in there and um i'm actually going to get rid of this because i remember seeing that you could just pass in like um a blank to basically get like an empty discord session object so i'm gonna do if error is nil i'm just gonna check for error fmt dot print line uh could not start session otherwise fmt or print line session and then uh discord gucci okay so right now we don't actually have this third party dependency installed there's a couple ways that we can pull it down now if i think if we just do go run it'll pull it anyway the better way to do it more consistently especially when you're both adding and removing dependencies you should do go mod tidy and that will actually um pull it in if you don't already have it and i believe if you're using go modules it will put it in in your your go path that you have defined from when you have go installed it puts in like a cache so if you already have this in your cache it doesn't actually pull it that specific version that you're using and if we should go look at our go dot mod you will see now there's a new line here it says require and it's that package we pulled in okay um i think you should and you also see now there's a go dot sum now if we i'm just going to cat it out so we can look at this and you'll see so here is the package i pulled in right well you'll notice there's there's some other things here why are these here well the common train of thought would be is that okay discord go probably relies on these for some reason which fair enough probably does um in fact if you want to find out why there's actually a way you can do that with the with the module command and go there so you can do go mod y dash m to find out like you're asking a question about a module and so let's find out why gorilla websockets there go to github.com gorilla web sockets socket spell and this says okay gorilla set starting from top down the the usually the top one is the thing that you're the with the module that you are in so this is your module your module is consuming discord go and discord go is consuming the websocket one that's that tells you why that module's there um what about the the crypto one let's find out about that one so let's get rid of all this goldlang.org asks crypto and see of course your module is consuming discord.gov which consumes this package called crypto slash nacl slash secret box don't know if that does but that's using it um so that helps you find out what these other packages are that will be installed along in your binary so let's also say for example that you want a specific version of that package right so as you see the newest version i believe it said it was version [Music] 20 0.21 which you'll notice that this is also using a semantic versioning it's uh modules i think actually more or less tries to like force you to use semantic versioning for a reason and if we go to tags let's see let's say i want um 0.20.3 specifically okay i believe if we want to do that we can do whatever do go get github.com vw marin discord dot go at version 0.20.3 like that you see it is pulling it because i did not have it cached locally and if we go look at our go mod this should be this has updated to indicate that hey you are no longer using a version the one we were using which is like 21.1 it's using the one we just told to do if you want to by default i believe it always pulls the latest another way to make sure that you're using the latest is instead of doing the whole semantic version you can actually just do the latest and that will always pull the latest version of it and you'll see it has updated the mod file to use 21.1 which is what we're just using let's double check there we go it's changed again okay so last thing that would probably be important is or two things um let's say that there may be times where uh you may want to vendor your dependencies uh if you use node and npm you you are well aware what vendoring is it basically uh downloads a local copy of your dependencies into your project this tends to be useful if you have like a lot of dependencies and you're using something like some kind of build automation pipeline pipeline and every time that your pipeline runs you don't want to have to pull you know maybe dozens or hundreds of packages down to do a build you just want the they're already there and just builds from them so the easy way to do this is actually very easy you just do go mod vendor and it will put a new directory here called vendor that has all of your a local copy of all of your dependencies there um i'm gonna go into lf and let's actually look at that so you'll see in the vendor folder we have the github.com which has our which is where the discord dot go and the gorilla packages come from and then golang.org is where the krypto one comes from and this also actually kind of gives you more detail about where each package is coming from so you'll see there's a line there it says explicit meaning like hey this is the dependency that i i am explicitly using in my package and then for each one of those you can see its dependencies so you can see that um let me just open this up okay there we go that websocket uh is using or this there's also or it was a discord.go is using this and that's using like all these sub packages from the crypto package uh so yeah that's how you get a a local copy of your packages installed so that you can just move them along in your pipeline and the last thing is well let's say that you've removed dependencies let's like let's go back into our main go let's say um i decided and i don't want to mess with this thing anymore so we're going to get rid of like i want us to do fmt.printline i'm just going to say hello world right and i'm just going to i'm just going to delete all this and all all this stuff gone bye it's gone so i'm gonna exit out of this now if we go to uh go.mod it's still in here right but we don't need it anymore we also don't need the um the vendor defenses anymore well remember that command we ran to uh pull and update our go mod file called uh tidy if you run that it also double checks to make sure whether anything that you have listed in your go mod file and your vendor and that's in your vendor directory is even still being used if it's not it just it cleans it all out i'm gonna run tidy you'll notice actually that's still there that's very weird go mod okay oh that's right so if you want to completely get rid of the vendor thing too um i don't know if there's an easier way to do this but if you run this now it's going to say there are no dependencies the vendor and it's going it's dead and you'll also see that our go mod again has nothing in here anymore it's been cleaned up and i think i guess ghost some would have yeah it's it's empty because there's no dependencies to checks them against but there you go that that is how you use good modules to manage your dependencies um i believe if you're wondering how to migrate from one of the older tools to modules so like dapper go depp or go benders whatever um i could be wrong but i believe in most of the cases if you run go mod init inside of a directory that has one of those some of those like common files those produce i think it actually reads them and actually translates them into a go mod file and then you can do the things where you run like go mod tidy to install everything cache it and update your go mod file and then you can just get rid of the old stuff and then let's basically like a painless transition uh i think they did that on purpose because they understood that like there's three or four other packages that people are using to manage dependencies and they want everybody on modules now because that's the you know the the built-in way in the tool chain to manage dependencies and they wanted that transition to be as painless as possible um if you liked the video be sure to uh follow subscribe depends on wherever you're watching us at um share the video if you think it would be helpful for someone else or you just you know just want to be a cool kid and share it with me it helped me out a lot man um and if you would like to support these videos being made i have links in the description of wherever this video is you do that as well and with that y'all come on back now and i'll see you next time [Music] you
Info
Channel: Donald Feury
Views: 17,813
Rating: undefined out of 5
Keywords: golang modules tutorial, go modules tutorial, donald feury golang, donald feury, go modules, go modules explained, golang modules, go modules local package, linux, programming, tutorial, feurious, golang tutorial, golang, go, go programming, golang programming, programming tutorial, go programming language, go programming language tutorial, go programming tutorial, interface, go language, golang basics, software development, package management in go, Go basics
Id: 9cV1KESTJRc
Channel Id: undefined
Length: 14min 55sec (895 seconds)
Published: Thu Jul 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.