Golang Packages & Scope Explained!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today's video is all about packages a girl is a pretty opinionated language when it comes to package and file management so if you've ever been confused and want to learn how to set up a project in go properly or at least i know enough for you to start writing some simple programs and keep on watching [Music] what's up ninjas and welcome back to golang dojo this channel is all about the go programming language and becoming her golan ninjas together for the next few dozen videos i'm dedicating all of the videos to go through some of the most popular technical books related to the golang so if you want to learn the go programming language but you hate reading technical books this channel is for you because i'll be doing that for you so make sure to join the golang dojo by hitting that subscribe button and let's get started now before we dive right into the code itself let's talk about some simple programming concepts like scope and then later bring them all together and talk about how they're being applied in the go ecosystem first of all what is scope with the assumption that you know what a variable is the scope of a variable would be where you can access and use that variable and in the goal environments development environments there are primarily three levels of a scope and they are block package and the in the entire universe and we're gonna talk about exactly what that means right now here we have a hello world program in intellij so if we try to run this holo program you can indeed see that it is printing out hello world and we just talked about that there are primarily three levels of a scope in go and they are block package and universe and let's start with a block first so block what that really means is between the curly brackets so if you were to declare a variable if you don't know how to declare a variable in go make sure to check out my previous videos so if you declare a variable assign that one into that variable and you try to print out the integer and run this program again you will be able to see that it is opening our hello world this line and it is also printing out the actual integer we can access at the variable integer because it was a previously declared within the same scope within the same block between these two curly brackets however if we actually try to declare a variable in a different scope in a different block between a different set of curly brackets and call that variable just call it diff here and assign a different value and we would not be able to access that variable outside of these two curly brackets because it's living in a different scope but it we can absolutely use that variable as long as we are trying to access it within these two same curly brackets here the interesting thing here is that we can actually change this variable name to integer as well even though it hasn't been it is going to be declared for the second time outside of these curly brackets because there's a variable name uh this variable integer here will only level to the extent of it of at least two curly brackets as soon as the computer tries to run instructions outside of these two kodi brackets we can reuse the exact same variable name again and if we actually try to run this program as you can see when we try to print out integer for the second time we are not getting the value for when it was it declared the very first time so that is the block level scope essentially local variables and let's talk about package level scope next so the next level up is going to be declaring a variable outside of the main function here and we haven't talked about function much yet so make sure to subscribe to this channel for future videos dedicated to functions so we can actually declare a variable let's call it i don't know one actually let's let's call it k and assign five here four let's call it five now we can actually call this a variable in a different functions besides address main if we have a non-main function here very very extremely creative namely here we can actually print out 5 here and we can print out 5 here as well obviously so if you have any experience in any other languages you can you will know that you can do this perfectly fine in other languages as well however what's a different about go is that if you declare a package level uh variable you can automatically use there's a variable in other files as long as they're in the exact same package here so if we were to declare a new go file and call it i don't know integers dot go here and it that integer has uh integers that daiko actually has the exact same package name here or in the same package if we actually have a declare a different variable here called three we would be able to use that uh three within this file but we can also use it in a different file we can use it maybe like here and if we actually try to print out you can see that it is a printing out three even though the variable is declared in a different file now you might be thinking especially if you have experience in other languages how can i actually declare a variable that can only be used in this one file but not accessible from other files and the answer to that is unfortunately you can't go actually puts a lot of emphasis on packages and so much so that even though you can create individual files but the variables that you declare within these individual files as long as they live outside of these functions you can access it from other files as long as the other files also live in the exact same package and it is also recommended that you create a directory directories or within that with a name that is the same as the package name and actually put these files that live in the same package of a main to put them into a directory call name now you might be thinking what if i actually have a different director i have a different package let's say that i don't know i would have a different directory called integer integers here and then my integer actually lives in a different directory but not in the same directory of main so let's put that here and let's see what the intellij id is going to do so it is going to move this integers and i go to the directory of integers here and also you can see that automatically intellij he has changed my package name to integers as it should be as it as a girl recommends the next good question would be if you remember we actually have this variable reference in a different file in a different package main.go and we if i actually click on the main.go right now we can see that it automatically imports there's a file path and we're going to talk about what exactly this file password comes from but as you can see it is it's importing a uh the directory but not the file but the but the importing the package but not the file itself it's importing the package and when we are trying to use the three in there's a different uh package it is going to called integers at data3 but you can also see that it is giving us an error here because it is a unexported variable 3. the reason for that is if you want to actually export a variable in a go that lives in a different package you would actually have to make sure that the variable actually has a um has it's a first a letter capitalize so if we go back to main dot go and if we change it to a three with a capitalized a t then we would be able to perfectly use that variable from a different package perfectly fine so if we want run there's a program again as you can see it is a printing out three perfectly fine and that is the third level of a scope let's do a small recap here the first first one the first level is going to be the block as long as you have a variable declared within the two curly brackets within the same block then that is at the very first scope and you would not be able to access variable declare in a block outside of that block and then the second scope would be a package scope as we as we talked about you can have individual files and all of the variables that you declare outside of the local blocks outside of the functions you would be able to access that variable as long as where you access it in uh the file that you are accessing it from is going to be the file living in the exact same package and the third level of a scope is going to be a universal scope as long as you have the first a character capitalize then you will be able to use that variable or function or we're going to talk about functions in future videos but you will be able to use that exported component in other packages one thing to note is that the best practice is to keep the scope as narrow as a possible when you are coding for example if you can actually declare a variable within these two curly brackets in the same block that you want to do so and not declared outside of other functions and if you can actually declare a variable outside of the functions but within the same package with a uncapitalized uh first a letter then you don't want it so that you would uh declare it as a exported variable that you that can be accessed anywhere else universally also if we go back to the main.go file we can see that there is a path here and we said that we're going to revisit this path and we're going to do exactly that right now so what the path of format is going to look like is actually going to be the host and where the the code is being hosted uh in this case is golandojo.com i don't actually host it but i just have it like that uh instead of promoting some other open choice platforms but and then the next thing that you would have to have following the host is a user or organization name and then you the next would be your project in my in the case of my current theory is the book go uh programming language and that is the project that i'm that i am in right now and then you can have all of the directories that you like preferably as flat as possible but uh you can definitely declare additional directories in order to organize your files your packages a little bit better and then at the very last is going to be your package name and in my case my package name would be uh integers that is the package that i am getting this exported variable from so this is a summarized walkthrough of how go manages packages and files and how the different components interact with each other in a go project obviously there are going to be other caveats but this is more than enough for you to set up your project in an organized manner according to the book go programming language a reminder that for the next few dozen videos i'll be going through some of the most popular technical books another sign related to the golang so if you want to learn the gold programming language but you hate reading technical books make sure to subscribe to this channel because i'll be doing all the work for you with that let's shout again in our next video [Music] you
Info
Channel: Golang Dojo
Views: 4,854
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go rest api, golang generics, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, golang packages, golang scopes, Golang Packages & Scope Explained, go packages, golang scope, go packages and modules, go modules, golang packages and modules, golang project structure, packages in go, packages in golang
Id: vYD9XWi_Xw8
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Sat Mar 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.