In this tutorial we are going to discuss about packages in Golang.
Packages are the most powerful part of the Go language. In the most basic terms, A package is nothing but a directory inside your Go workspace containing one or more Go source files, or other Go packages.
Packages enable code reusability across the Go applications. The naming convention for Go package is to use the name of the system directory where we are putting our Go source files.
Within a single folder, the package name will be same for the all source files which belong to that directory.
Every Go source file belongs to a package. To declare a source file to be part of a package, we use the following syntax
The above package declaration must be the first line of code in your Go source file. All the functions, types, and variables defined in your Go source file become part of the declared package.
There are 2 types of packages available in Golang.
Executable type of package is the one that when compiled splits out an actual runnable file file or an executable file. Just like what we saw when we executed the go build command at our command line.
Remember when we ran go build hello.go it spit out this hello file right here which we were then able to run and execute.
So this file right here was created specifically because we created a executable type package. Executable packages are usually used for actually doing something.
And that’s primarily what we are going to be do doing in these tutorials. You know we are going to be writing programs that we can run and we can use them to accomplish tasks.
We also have access to reusable packages and you can think of these as being like code dependencies or libraries.
These are packages that are not used to say like double click on an execute. Instead we put in a lot of reusable logic or helper functions or stuff that will just help us reuse some code on future projects in the future.
So you might be a bit curious how do we know if we are making an executable package or a reusable one. So how do we know when we’re making one or the other.
If you look at our source code file clearly there’s nothing really inside of here that says oh yeah spit out some executable file when you compile me.
So how do we know when we’re making one or the other. Well it’s actually a little bit tricky. Remember that we called our package with the name of main. So the very first law line for us inside of our files said package main.
It’s actually the name of the package that you use that determines whether you are making an executable or dependency type package.
So specifically the word main is used to make an executable type package. So we took package main we ran go build on it and it spit out a file called hello or hello.exe if you’re on Windows OS.
If you are used any other name like package abc then we ran go build on it. It wouldn’t out an actual executable file.
So the word package main is sacred. It’s one that we use only when we are making a package that we want to spit out some runnable file.
You and I are going to be making projects that primarily are using the name package mean because we generally always want to be creating something that we can immediately run and test.
However if we were trying to make some library of reusable code or if we wanted to make some project that we can share with others so they can use our code on their own project that’s when we would start using a more specialized package name.
Now to summarize Basically whenever we see the word package main that means we are making an executable package any other name whatsoever means we’re making a reusable or dependency type package.
Now the last thing I want to tell you about this last thing is that anytime we make an executable package it must always have a function inside of it called main as well.