GoLang Packages and Modules | Beginners Go Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on welcome back to nerd academy i'm your host james and in this video we're going to learn about packages and go so let's go a large part of software engineering is the ability to reuse code this principle is known as dry don't repeat yourself the most basic block in go of reusable code is a function where you have your keyword func your method name your arguments that you pass to it and any return types and inside the curly braces uh just the code that's going to do something and the next step in reusable code and go would be packages a package is really nothing more than a bunch of go source files and may include other packages let's go jump into our code editor where we can take a much closer look let's go so usually when i start i already have my main.go already kind of set up and usually some actually third-party packages pulled in so we're gonna kind of step through it all so we kind of can understand how it all works so i'm gonna create my main.go file and as you can already see there's an error because every go file has to be a part of some package and since this is going to be our entry point to our program our main.go it could be anything but i just call it main.go because this is our main executable it has to be package main as you can see that little error went away and the entry point into every executable go code is funk main so a package that i use quite often is fmt so let's import that and the error we get here is imported but not used so another rule and go if you import the package you have to use it if you declare something it has to be used because go doesn't want any unused stuff inside the program that will make bloat it tries to be as slim as possible and as fast as possible so the fmt package also has a bunch of reusable methods such as print line print f sprint f so let's do that right now so let's create a greeting and we'll call we'll use fmt s printf which just kind of formats a string for you we'll say hello james then we'll use the fmt print line and we'll just print the greeting out and now we should be able to run it with no issues go run main.go and hello james all right now prior to go 1.11 everything you did i believe had to be in your go source directory i started using go i think 1.14 i want to say i don't remember exactly but now we have modules so when we import third-party libraries we have to do something else because we're in our go root or it should be able to see it because i've already have this package we're about to pull down is already in my go source which would be for me is at home go i think package get oh mod github gorilla mux it's already there but about to show you it's not going to be able to find it let's make it just a quick test of a mux server so the package is github.com gorilla and mox if we hover over this first one it says imported but not used once again the same error is fmt this time it says could not import because i cannot find the package even though i just said it was in it was in my source directory it's not finding it because we're in a module so to do that we do go to a terminal we run go mod init and the url is where if you were going to host this where this package could be found for us we'll do example.com packages so this creates a go mod file if we open that go mod file up it has our module with our name we just put in there and then any dependencies and for us we're using go 1.17 and then after this any other dependencies that we use will be here but to get those dependencies here we have to run go mod tidy as you can see it's saying right there and in our terminal also says to add module requirements and sums go mod tidy so let's run that now and there you go it found that gorilla mux and imported it and if we go back to our go mod file you can see it added a requirement for github.com gorilla slash mux and the version go back to our main.go now it only complains about being imported but not used so let's use it make a quick router new router and then we'll use the listen and serve and 9000 and r we're not really gonna do anything with it but as you can see now there's no errors now if we run it once again no errors now it's just sitting and waiting for that mux http server control c to enter to interrupt and stop it and let's go look over the go.sum so this is kind of like a chain of dependencies that each package you import would use or is required and in our case it doesn't look like it uses anything extra it probably uses the net http library for just about everything it's not importing any other packages on top of it so that's kind of useful to know just in case there's something in there that may be affecting or you may want to further research and figure out if there's any security vulnerabilities in your whole chain and then let's say you were you didn't want to use the package you can obviously just comment it out we'll just comment it out here and then we run let's go back to the way we started but our go mod file still has the mux in here the gorilla mux dependency and so does our go.sum to get rid of that we just run go mod tidy once again if we go back to our go.sum it's empty here we move those and our go.mud go go.mod no longer has any of those dependencies listed here either now let's create our own little package and use it in our main.go so we're gonna do some really fancy i'll make a utilities package we'll call this one strings.go cause we're gonna do something with strings and we're gonna call this packageutil now the the normal convention is your folder name and your package name should probably be the same let's do something super super difficult we're going to make a function let's get the string length of a string you could pass in called s for string return an integer and we're going to return using the standard library the length method which takes in any type and we'll pass that in boom so now let's say we want to use that method we go back to our main.go and we want format that print f the length of greeting is preset d for digit and we're going to use util uh i'm not even getting the intellisense what was that string length huh greeting that's saying undeclared name we have to import it so most in most other languages you probably can just do util right no that's not working either what we have to do is use our entire url that when we use when we ran git git when we ran go mod init so if we go back to our go.mod we can see our module is example.com packages now to get our util we go example.com packages slash util and since we since we're inside this directory it knows the util is there and now we're not getting any any compile errors everything should be fine let's run it because sometimes just depending on the code errors isn't because this isn't a full integrated development environment there you go works just fine alright so let's talk about scope for just a second in structs you probably already know if you want to use the struct field you have to use an upper case the same is going to be true for packages so if we go back to our function here and put lowercase s string length if we go back to remain.go there's an error saying it's not declared by a package util i mean if we go back here it is declared right but it has to be uppercase for it to be considered exported so that's exported there if we use an uppercase another thing about scope at the package level we can have a variable in our package util we'll call it greeting we'll just say it's hello we'll keep it lower case and let's create another file we'll just call this fun.go because we're going to have fun package util we can get the greeting return the string and we'll return the return the greeting now if we go to main.go we can call that method print line util dot get greeting and we can run it and we get that string hello but now if we just want to if we want to try just to get the greeting it's not you know intellisense isn't helping us although it does show us right here the actual greeting in the string but the same greeting is not exported by package util so if you're within the same package you can use any variable that's declared kind of globally at the package level so since we declare this in the strings.go file it's in the package util where if we go to the fun dot go we're still on package util so that is available in this go file but not outside of the package because we did not export it with an upper case so let's change that back to get our greeting and that is pretty much in a nutshell the basics of packages in go all right that wasn't so bad now was it and if you made it this far on this video i personally want to thank you it means a lot to me that you've watched this far so if you can do me a favor if you got anything on this video just go ahead hit the like button for me help out that algorithm and remember always keep learning and if you want to watch more about go watch this video right here and we'll see you in the next one [Music] you
Info
Channel: NerdCademy
Views: 507
Rating: undefined out of 5
Keywords: software development, software development tutorial, programming tutorial, go programming tutorial, golang programming tutorial, go packages and modules, golang packages and modules, go packages, golang packages, go packages tutorial, golang packages tutorial, software development process, programming, software developer, go mod init, go mod tidy, dry principles
Id: nLaxs5w9bZc
Channel Id: undefined
Length: 10min 26sec (626 seconds)
Published: Mon Nov 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.