If you are familiar to languages like Java or NodeJS, then you might be quite familiar with packages. A package is nothing but a directory with some code files, which exposes different variables (features) from a single point of reference. Let me explain, what that means.
Imagine you have more than a thousand functions which you need constantly while working on any project. Some of these functions have common behavior. For example, toUpperCase
and toLowerCase
function transforms case of a string
, so you write them in a single file (probably case.go). There are other functions which does some other operations on string
data type, so you write them in separate file as well.
Since you have many files which do something with string
data type, so you created a directory named string
and put all string
related files into it. Finally, you put all of these directories in one parent directory which will be your package. The whole package structure looks like below.
package-name
├── string
| ├── case.go
| ├── trim.go
| └── misc.go
└── number
├── arithmetics.go
└── primes.go
I will explain thoroughly, how we can import functions and variables from a package and how everything blends together to form a package, but for now, imagine your package as a directory containing .go
files.
Every Go program must be a part of some package. As discussed in Getting started with Go lesson, a standalone executable Go program must have package main
declaration. If a program is part of main package, then go install
will create a binary file; which upon execution calls main
function of the program. If a program is part of package other than main
, then a package archive file is created with go install
command. Don’t worry, I will explain all this in upcoming topics.
Let’s create an executable package. As we know, to create a binary executable file, we need our program to be a part of main
package and it must have main
function which is entry point of execution.
A package name is name of the directory contained in src
directory. In above case, app
is the package since app
is the child directory of src
directory. Hence, go install app
command looked for app
sub-directory inside src
directory of GOPATH
. Then it compiled the package and created app
binary executable file inside bin
directory which should be executable from terminal since bin
directory in the PATH
.
Package declaration which should be first line of code like package main
in above example, can be different than package name. Hence, you might find some packages where package name (name of the directory) is different than package declaration. When you import a package, package declaration is used to create package reference variable, explained later in the article.
go install <package>
command looks for any file with main
package declaration inside given package
directory. If it finds a file, then Go knows this is an executable program and it needs to create a binary file. A package can have many files but only one file with main
function, since that file will be entry point of the execution.
If a package does not contain file with main
package declaration, then Go creates a package archive (.a
) file inside pkg
directory.
Since, app
is not an executable package, it created app.a
file inside pkg
directory. We can not execute this file as it’s not a binary file.
Package naming convention
Go community recommends to use plain and simple names for packages. For example, strutils
for string utility functions or http
for HTTP requests related functions. A package names with under_scores
, hy-phens
or mixedCaps
should be avoided.