Package

Definition and Types

Definition

  • A package is nothing but a collection of common source code files. So when you see the word package in go, you can think of a package as being like a project or a workspace.

  • For example, if you and I are working on one discrete application like we're working on one app right now, we would traditionally be creating one single package. A package can have many related files inside of it, each file ending with a file extension of GO. The only requirement for every file inside a package is that the very first line of each file must start with the package name.

Types

There are two types of packages in Go.

  1. Executable type

  2. Reusable type.

Executable type: An executable type of package is one that when compiled spits out an actual runnable file or an executable file, It is like running the go build command at our command line and outputs the display. So specifically, the word main is used to make an executable-type package. If you use any package with the word main that is ready to execute and outputs the result immediately.

Reusable type: An reusable type of package is like you can think of these as being like code dependencies or libraries. These are packages that are not used to say like double click or run the command and execute. Instead, we put in a lot of reusable logic or helper functions or stuff that will just help us reuse. Let say, if you want to use any go libraries or dependency packages those are all come under reusable type packages.

Last updated