So we're coming along on our tour of the go language.
We know how to use variables.
We know how to make decisions with an if statement or a switch statement.
We've had a look at pointers.
We've had a look at interfaces, custom data types, maps, slices.
And it's time to look at another important concept known as packages.
And if you've worked in any other programming language, you're familiar with use of packages.
Java uses them and calls them packages, imports, things.
And we've been using packages right along.
Whether you're aware of whether you're aware of it or not, I have an empty mango here.
And if I try to print to the log log print line, hello, it actually imports a package for me.
And in Goaland, if I roll over it, it'll tell me what the package is called, log and how to use it,
import log and then describe it.
Well, we can also create our own packages log as part of the standard library.
We don't have to import it or we don't have to define it.
It's already defined for us, but we can actually define our own and we do that in a really simple way.
I'm going to show you how to do this again in a later lecture, but it's good to get our feet wet right
now.
We need to tell our Idy and go that we're using packages or go module's go module's is the way we use
packages.
Now, he used to be you'd have to define a gopalnath environment variable and put things in certain
folders and it was a real pain.
It's much easier now.
So I'm going to open my terminal and type go mode in it and then I'm going to give my the package I'm
working on, which is my program.
Aname.
Conventionally you start you name things by prefixing it with the name of the git repository where it's
going to live.
You can call it whatever you want, but it's useful even if you don't push to GitHub or BitBucket or
any repository to use this naming convention because you might push at some point GitHub dot com.
My username is Chiseller and I'll aim this package, my nice program.
So it created it.
Now, when it does that, it creates a go mod file and the mod file is automatically created by that
command we just typed, and inside of it it tells me what version of go I'm using one point one five.
You might be a later version, but it doesn't matter.
And it names this module, whatever I've just named it.
And there's another thing that I sometimes have to fight with goaland to get it.
Recognizing it, you go to goaland preferences if you're in goal end and search for goal modules so
you can type modules up here and modules and it will show it.
And there it is.
And make sure that you have this checked enable go modules and apply and then click, OK, and there's
probably similar steps to follow in visual studio code, but maybe Visual Studio Code is smart enough
to do it without being kicked three or four times, which apparently goaland wants and requires.
Once I've done that, I can start creating my own packages and I do that by going to my root level of
the directory, creating a new directory, the root level of my project, creating a new directory.
I'm going to call my helpers and it creates a folder and inside of that will create a go file, which
I will call helper Stocco.
And Goaland opened it for me automatically and you'll see that it names this package.
Whatever the name of this directory is, it happens to be called helper's.
And I can do things like define a type type some type which will just be a struct and it'll have two
fields, one for type name, which is a string and one for type number which is in it.
And I'm actually using these, I'm just creating a type that I can use in my main goal.
So back in my main go file, I can actually use that type.
How do I do that via my var?
I'm just naming a variable and it's a type, some type and it automatically shows me that helper's some
type is available and when I choose that it imports it and it imports it.
Using the package name.
I specified what I wrote Gomaa in it and then it names that package.
Which one of you important.
I can create another package by creating another directory.
I haven't done anything with my var but the fact is I've imported it so I can do things like my dot
type name is equal to some name and then print out to the log logged print line my va dot type name.
And if I run this go run mango it will work and there it is printed out.
So that's what packages are.
And the thing is you don't just define types in packages.
I can also in my helper defined functions that I can call by importing the helpers package and then
I have access to everything in that package and that's all there is to it.
So packages are incredibly useful, incredibly powerful, a nice way of getting our code organized into
logical groupings and will be using those regularly as time goes on.
So you're going to go through the same exercise in the next section when we start actually building
things.
But it's important to have at least some concept of what a package is right now.