#golang #modules #striversity Switching to and using Go Modules - The Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to go on the road and in this video I want to talk about go module now if you've been following along all this well the last video was on how to install MongoDB and I promised that the next video would be how we use MongoDB from go well why the change that's because I've been using go modules and I think it is absolutely fantastic and I think it's going to help all of us if we sort of switch to Google forget sort of if we switch the go module okay I'm not gonna pull any strings let's just say I would really like you to switch to go modules and so this video I will show you why so here I am in my go run directory and I've written a simple program so let me start it up and I'll show you what I mean I figure I just a little straight for you why I think that cold lag core module this so good so the new the program I've written first to illustrate go module is this one called love test okay and just sort of a very simple program so we can save some time I simply have a main application and I'm going to write some log messages to log lines using the print line function and then the last one is to save fatal log fatal and it should cause my program to exit I'm using the standard go package let me run the code and let's see alright so going to our love test directory same exact code I haven't changed it and so let's to go run and may and blah so this is exactly we expect it gives us the time - run and date and a printer them the message know if you come from a Java environment of writing love messages you sort of expect to see things that allowed messages that are info versus debug for systrace versus what's warning and then of course if you have a fatal one well sure the program would might be tell you that's gonna exit but you sort of want that indication from the log message itself like here we have no indication that this is whether this is informational only or it's a warning or for trace the bucket so for example if I want that sort of thing I'll have to include it myself I have to say something like you know info something like that and this is a warning for example alright something like that and then now if I rerun my program then I get that output so why should I do this why don't I just invoke you know love that info for example and have it prepend that string for me and if it's a warning I should be able to say love that warned right that is ideally what I would like and but this standard log package does not have that it does not have your NATO's functions but there is a lot package that as that and that log package is from it's called Lagos and let me put that in a place instead and so you've noticed that I've been using log rows instead of the standard log package but because I still want to use to you know reference here for the package name log well I just overwrite it because otherwise I'd have to go through a write Lagos progress because that would be imported as log R Us so I understand and put it with an alias cloud and then once that save all the warning goes away and so now if I rerun the same program I'll leave it here so you can compare the result you can see now this is much more clear to me immediately what is the info message what's a warning and then here's my fatal message with you know highlighted in red so I really really like the output of Lagos and there are many other features to it that I don't even worry using because this alone is just really nice for me okay so what does this have to go with coal module well someone pointed out that when they try to build my application and run it it fail and in reason they fail is because in order to use Lagos you have to get it for us you have to do go get and then say that well you wanted to use love-love rust now because I have it my application work so let me show you what happens if I remove this and in the land of go module hope things change so we know that though when I do go get it gets the source and it puts it in my goal pad territory so what is my go pack well let's just see so env and a psychopath let's acts for what my co pad is oh and that's it so let's remove the Brian bias RF and I will assess my home that actually should go slash source and github and it's sir you and and so let's remove that okay now if I rerun my program is still gonna work because it's also in my package leverage to being compiled and placed in my package directory right so Darwin come on Mac and so github and sir you spend and it's there okay so I can have that remove also so let's remove that all right so now if I try to rerun my program it should fail so let's see let me clear my screen a bit of it and we'll run main and of course it says I do not have this so this is exactly what you would get if you didn't previously install this package and so that's not nice because know when I write example and I use MongoDB package no you have to go get that wouldn't it be nice if with the code I had some way of saying what the dependencies are for this application see essentially we want dependency management and so that is fairly easy so the first thing you have to do is install go version one that 11 alright so that's the first thing you have to do in order to get the nice stuff tabs so if you do that cool mud how you type in it it should create a virtual file for you but here is failing to create that module for me because there is this environmental variable that is set to auto wins once you install go version one that you live in and what autumn means is that if I'm in a go path do not use go module but if I'm outside of my go path which I'm not because I'm inside of the code directory source and blah blah blah so I'm in this go path which I showed you before with my go path is well so no it wouldn't use it because this variable is set to auto if I want to force the use of go module while I'm still in my go path well I have to set this to on so there's another way I can do this I can go to a directory outside of my goal path so let's go to here for example and let's copy actually let me see the back there and go up one directory and then I do see P minus R I like to call of test and called put it in my home directory so I'll go to my own directory and I'm going to lock test now remember I still only have my main that go program so let me clear up my screen a little bit and so I want to use go module if I say go mod in it well assume it can determine the path I want to use like the module path the module path are sort of like the git repository that you intend to use and if you already had a git repository setup it will just simply use that so I'll just give it something and I'll call it this is you know github that come for which like barrel straight varsity for example and go on the run for example and maybe I could one may call it love test if I want but go module is so flexible that really you can have multiple things but in this example I call it love love that test and so no it created a good at module file for me it did not create anything else right I had my main that go it created school module file so what is in this core module file so let's take a look so if I can go mod you'll see that all it says it says module and then it has the kid up had a tie tie still doesn't say anything about the dependencies that my main that go program but you're still here neat which is you know Lagos so if I do now go run I type main that go again haven't changed anything other than type initialized a coded module file and by doing that now notice all my program run successfully I did not have to install anything I did not say go get and what happened well a lot of things actually if we just do a cat on that go dat module file again will see that no it says that Oh looked at the file main that go so that I didn't have it add it to this file and so now if I check in this code with the gold that mod file and main that go your if you use it go version one that 11 you simply to say go run and guess what your program will run and you don't have to worry about downloading anything so that's really cool but then you're probably thinking well verow for this to work I had to go outside and maybe I have my hold I retrieve my source package and all that stuff set up already I don't want to start developing somewhere else so let me get rid of this then and go back to okay and so I'm back to my Quran project directory and so there's my love test so that's going to it I still have the same application that I had before and I still do not have a go that module file here if I try to type go module in it again go module and in it it will tell me that I am inside a directory that has go pad set so it's disabled so what I can do is do an export go Metro equals on to force it on so instead of having the auto auto means that if I'm a psychopath then turn off go module if I'm outside of go path then use go module now I'm saying I won't go module on and I really don't care if you can go pad I still want you to use it and so here I will say go in it go mod in it know I'm taking off the github and all that stuff because this project is already on the github so it automatically uses the same path that I would have typed that's because this project is already on the erosion control right so now when I type and go run main you know of course will work now you might say I'm overly download it and cash it before yes it did so let's remove all of that so minus RM I remember I had my package directory PKG so let's remove that oh it's enemy at how things are I don't have permission to remove certain things and that's because some of these files that were downloaded by the package were placed in there as read-only so let's change mod permission on those things chmod and I'll change out the permission to give myself read permission and then now I'll go remove it that's annoyed a little bit okay so it's gone and now we should expect that my current command should again work and notice because everything was removed when I say go run and add this file the module file it went it found all the dependencies downloaded it and if we look again it created that module files which we can see here there is and it put the same requirements in now you might see another file which is this go some file and basically what it's doing is just keeping a check sum of all the module it downloaded so the next time it needs to download something is gonna check and see well do I have that already and if I do that I don't need to don't worry but you don't have to worry about managing to go some file or even putting it on the Fortran control because we'll just create it the only thing you really need is the gold module and of course your source know things that even I tell you go module is really really cool so to get one other thing before I end this so as you can see here I have goal a goal my file and it goes some so I will remove both of them okay still just have my main program I'll go up one directory so this is represent the root of all my projects this is what I've been fortunate when putting in version control we notice because this is where my git project is right and I can prove that by saying cat that get that's perfect and then you can see this points to the remote repository for you know this get lab repo and so here is where I'll run might go mod in it and so now because I running from this directory notice the name that it used instead it did not use the log run which is a sub-directory I was in well even though I put go mod in the parent directory I can still go back to my log test directory and I have a dependency in this directory remember I still have a dependency on Lagos and let's just make sure that we don't have anything store from before so I want to change mod let's remove I'll remove all of that minus RF so let's remove everything so we know so we don't have anything we don't have any love Ross anywhere or we should also remove from the source directory so source github doesn't look like I have anything for la crosse in this directory so I don't want to remove the source directory cut out remove all of my code so all right so let me show you this one last thing so I still have my application here that uses module that uses lock rust and then within the parent directory is where I say my module root is but still from within this directory I can still say born run main let go and notice how it still works that's because wherever I run my code from I'm used in module it would keep going it will check in that directory for us for a good at module file if it doesn't find one it goes up to the parent if it finds one there then it uses it if it doesn't it keep going up and so on which really would but this means is that modules are super flexible because if I created a directory and let's call it a b c and so know i have you know something that looked like this if i go down to the C directory and I have some code and I if I like I can put another go that mod file there to manage which modules and versions of different packages should be used from C don't right and so anything they get compiled below c directory would use the good at module file there so for example it's just touch one touch ABC c let's put it at site will ever be so go that mod so if I did this what I'm saying is that since go that mod is inside of the B directory anything that gets compiled and B or any child of B will use the rules specify in disco that mod in this module okay don't put anything in there yet but you imagine that I had you know of course I went into this territory and had in module name and so on so let's see a B and let me just remove like over that much will have it created automatically for me by saying go mark in it and that should create a module for me and as you can see it created it with the name lock test for such a B so from from now on any cool I could I read right below this directory or its subdirectory would be governed by this code at module file so you could have module within module and that's what I think this is so great because it makes it super easy no for the author of piece of code to just provide the code and know that how it can be built correctly by somebody else who includes it so that's what I think you should immediately if you haven't already installed cool 1.11 and then start using modules alright so in terms of reading material there's some stuff over there that you can read and the easiest way to get to it is to go to the Gulag webpage go to download go and of course you can download the version of google that you live in for your operating system and after you've done that but if you want to read more in it then you should go to release history and then click here and then release notes too so go one that 11 release notes and then it tells you all about some of the packages here and this is basically how that the go module works and you can there's a whole lot more to it ok another thing that you might want to do is to go check out this documentation on new concepts and well actually it's not there it's Russ Carter wrote a blog post and where is it so here it is this website and this sort of gives you the nice background on why we need code modules and all the features and so on of it so how it works with you know versions and semantic versioning and there's so much more to just that really I cannot cover it all and I'm still learning myself but definitely go check this out so I think that's it so take care see in the next video thanks for your time [Music]
Info
Channel: Vilito Exquisitus
Views: 30,649
Rating: undefined out of 5
Keywords: verrol, golang, go programming language, learning, tutorial, vilito, vilito.exquisitus, striversity, using gorilla mux web socket in go (golang), web application programming in go (golang), web application programming in go (golfing), using sever sent event (SSE) in go (golang) web applications, using web sockets in go (golang) web applications
Id: B0EjcYaBm9A
Channel Id: undefined
Length: 19min 56sec (1196 seconds)
Published: Sun Oct 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.