Custom Packages in Go Language

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to my another video tutorial about go so in this video we are going to talk about go package management or actually go packages like how do you create your own packages and how do you import them and some of the inner details about how they work of course not gonna go into very details just some overalls but at the end we're gonna be understanding we're gonna understood understand how things work and you can develop your own packages so the first thing that let me see where am i okay great so currently we don't have much here except for us going to go packages folder that I just created [Music] so let's go ahead and create a new file here and then so and then that's its it makes an example and like it's used some existing packages for a sample and then we're gonna create an own package so I'm gonna use go code for that and it's something I like the Dakota editor of course if it doesn't fail watch it failed for some reason sorry you know actually I should have been working fine I know let's try again okay now it works so here we have make it bigger so normally like the way you use packages right you know not yet so you like first of all you define this is vitam a the package maintence paschal package you have to have it to create executable file and then we have this main packet main function which is another special function that you have to create executable file and princeton if i want to use a package i use i say import and then FMT so there's so there are a couple places where you're go go installation we'll look at the first one is its own directory special directories and the second one is it will look at your go path and then try to find the packages that are installed on your computer you can use go get which is a go tool command that allows you to download packages third-party packages or libraries from internet so that you can use them in your own projects which is going to be our next video topic but today i we're just gonna start off with the understanding how the packages work and have you create your own packages so that it can create a better organization better structure for your project in case you want to like let's say you want to have some anterior or third three-tier architecture or some code reusability between other packages and so on the packages are like one of i are how the go gives you encapsulation and abstraction we have private symbols at package level like normally we have this let's say if you are coming from C sharp or Java for instance you have private methods or private members in your class so it is at the class level of course you can also do like if you're in c-sharp for instance you can do like namespace level or you can do actually assembly level and so on right here we only have this package level abstraction so let's go ahead and continue using our package and we say println and then it's pretty easy typical hello world and let's take this and I'm just gonna go ahead and run this project and you see it's we have this hello world and now the way you do something public is you make it a capital which we're gonna see later on the video but the important thing that you need to remember is that when you import a package you use a directory path that's important it is not the package name you are importing you are import Europe you are using the path of where your package is located which is a directory path right but when you when you use the symbols from that path you actually use the package name which is FMT it's a good practice to have your directory path and your package name as the same which we're gonna see how they look like so let's go ahead and create a new folder at this point and I'm just gonna say touch I'm sorry make directory and let's say utility a little more don't use packages package names like utility helper and stuff like that because they don't make much sense I mean you can have a lot of things on those utility things and you make them complicated big and clumsy instead of you know naming them like utility just give them a better concise and more cohesive names in this example I'm not gonna care about that but don't do what I do here which don't give your packages like utility or stuff like that names like that alright so let's go ahead and create a new on the dad let's go ahead and say touch some stuff let's say strings go oops sorry let's move this guy strings that go to utility because this house should be actually a new utility and then let's create another touch another file here and say ends I don't I'm just making those ups so they're not really a useful name but what I'm trying to show you is that you can you can have multiple files under the same package or same folder with the same package name mind you that you cannot have multiple packages or different package names under the same directory so you have to have so it's basically one package per directory that's important but you can have a different name a different package name than your directory name and we're just gonna see how that looks like so let's go ahead and open the whole folder or whole folder let's go say code and you see that I have this guy and let's actually do utility here because I said it is a good practice so I can have function I'm gonna I'm gonna make it public for this one also let's say get name okay I'm gonna have another name another function here let's make it private so in fact let's do it under another file so let's go into the strings okay and then have package utility and then this time I'm gonna say get package name and we're gonna see how that works at this moment and this gonna return let's say utility I don't care and now I'm able to call that thing get package name you see that because of course we need to define a turn type here so string and string and that has to have a string type too and you see that even though ease we have like multiple different files go traits them as a big hole big hole file which is good which is like classes under the name same under the same namespace so you can just use them like that but not exactly like classes but whatever so at the end of the day you know all those things will be a big thing under the same package so let's go ahead on another and another one here I'm just gonna say I don't know I'm gonna get get my name for instance okay and that turns a string and my name is Tariq Guney okay and so I'm just gonna go into main and the way you import packages I mean you can of course do this or separately or by the way I mean you can see go packages okay packages and utility oops utility my goodness of course you have to use the package otherwise go tools we will remove it go tools will remove any unused packages soon to say empty println okay and say utility and now you can access you can see get name and get my name and get name even though there are from different files but they are under the same package and the packages and go gives you the abstraction as I told you before so let's say this and I'm just gonna copy the entire thing I'm just gonna paste it here oops what happened yeah okay so let's say get my name okay and then let's run this guy and see how that looks oops yeah so many commands here so we have this hello world artillery and tarconi so it is important to know that you can as I said before we can have a different name here so let's say target package okay and then you'll see how that affects our stuff we still use the same import thing which is an absolute there is no relative import path in go you have to use an absolute path which starts from Europe your parent directory and the other directories that you like your package directories and then now here you see that if I run this guy let's see how that reacts toward that change and says hey there's no utility package and that's important imported and not used by the way utility as toric and so it means it says you have a package named Tariq under this utility and so if I said Tariq here you see that I am going to be making this work so if I run this again same thing but as I said don't do this I mean it's it's confusing so you don't really see you know what uses what unless you have an advanced editor maybe new IntelliJ stuff or whatever so it's recommended is a better in a best practice to do to give you a name like utility here so that people understand which off the imports that your reason here okay so let's cover that guy so of course there are cases where I should have applied that to you this of course our cases were you may have but I before I explain that let me go ahead and do one more quick stuff and underneath this utility I'm gonna create another folder and I'm gonna say helper so that we can see how you package have you named your packages and let's underneath let's say a new file I'm gonna say my helper dot go and when you package ready name your package remember to package or name it like that so remember that you are going to use it you don't have to but it's a good practice you're gonna use the innermost directory name as your package name so if I want to let's say do something get name okay string see it is private so it's public because it starts with capital G so there's no private and public key words you have to use a you have this a specific naming format or a cases like this that capital G means it's public lowercase G means it is private jet like I don't you don't have to specifically specific tax you don't have to add extra keys or extra characters to define what is a is private or not so let's return some things a helper so when you use this guy the way you use it as a similar go packages if you can type I can't utility and helper now I can say very similar I can say oops oh my goodness okay now I can say helper and that give me change it and that gives me the package and I can easily say okay this is coming from this guy of course there are problems like some it will deflect someone else in a third-party library name their packages the way in in the same name that you named your package so let's say they named it helper so there is a very easy way of changing that information and you can just use alias and I can say my helper for instance my helper and now I can use my helper to here see I can just simply change the package name to my helper so that I can avoid any conflict with other packages let's have use that and of course you don't have to say import all the time you can just move this guy's okay and let's just use this parenthesis and I'll perform at this like go visual city ago runs go tools special go tools depending upon the action you take first if you save it and actually run some tools behind the scenes and formats your stuff and removes any unused imports and so on as you saw before and like that so see that you can still do this without less import captors and anyway so they can do this that's how the package sexually work and the good thing is that though when you build this guy let's look at the actual something real quick why don't you run this let's run this okay it actually builds the project as well and that's fast you see that it's real fast just builds it wait works let's go ahead and look at something more before we end this video and I'm just gonna say city one more okay this is how you know how the recommended go workspace look like so you have all of your projects on their source and you have your packages on their pkg which is like third-party packages usually actually there are like object files in there you'll see what they look like and then the bin and the bean is where you when you'd say go install it actually installs the actual builds your project and put the executable file if there's any into this bin directory and you can be able to you know see that there you can copy it publish it whatever you like it but it's there so let's go into pkg alright its package means package and then let's see what we have in there see that that's the architecture that my on I am on it's just DARPA AMD I'm using Mac if you're using it even though it's got it was gonna change but nevertheless it's still there it's different for my case and let's go into that okay and see so you see that go packages and is the the the project they were working on here you see this go packages and that's we have this go packages and look into this and have see the how that looks like go go packages see that we have this utility a and then if you look at the utility you see like helper a and all those things this is this is the you see the utility and utilities our package we developed right and the helper is another package we developed so when you do that the way go spits up your compilation is that it actually creates objects files so it doesn't have to recompile everything if it's not changed and they if you look at the inside of this basically I put all of your packages within this PKG folder each time you built it and then if it's already built and there is no change in it it's not going to build it again so if you look at the how that looks of that object file looks and the object files guys are like a pre compiled files and a linker at the end a linker will link all of them link them all together into one executable file or one other file whatever let's look at how it looks like let's say help with that a actually we're in 2d you see that we don't it didn't put your main stuff here because it's a different thing we just put them it puts all the packages third-party packages and your packages your your custom packages see it's that looks like a couple things here I mean there's some metadata here it it gives us well symbols like get name you see that and some other stuff it's not really a human readable but it has some information that go needs not you and it knows you know it reads it and takes action based on based on information here but you see that it actually stores the symbols as well it's like a header file or header information in if you look at the if you use if you were if you are coming from stay sharp you you have this tool il al TASM which is il the assembler and that allows you to link into your dll or assemblies and see you know what of information its source and one of the information is the tables are and the tables contain or include whatever store information about your assembly like the types or you expose the types within that assembly and so for so on so forth so this is similar to that okay so let's get out here and your source is or in in your source but let's do one more thing before we leave is this is that quick environment variable I set up for myself so that I can easily access the source code directory of my go and then from here we'll say go packages ok packages I can type it correctly so here you're in packages and if I say go install ok I did something great and now I can go to pack bin and let's look at the go packages that we have here you see that go packages and I'm just gonna run it and we have as HelloWorld utility Tariq knee and helper so it is an executable file of course if you were in Windows we were gonna get an executable file with with an extension dot exe X e right alright so guys that's pretty much all you need to learn you to know about packages and how you can use them in our next video we are going to learn how we can actually use and download third-party packages from different sources so until then you have a great day
Info
Channel: Tarik Guney
Views: 12,528
Rating: undefined out of 5
Keywords: golang, go, packages, go programming, google
Id: GnEviKt7U0E
Channel Id: undefined
Length: 20min 52sec (1252 seconds)
Published: Tue Oct 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.