Go Q & A


How are packages organized in Go?

In Go, packages are used to organize and structure code into reusable and maintainable units. A package in Go is a collection of Go source files that are grouped together in a directory with the same package name. Packages encapsulate related functionality and provide a namespace for declaring types, functions, variables, and constants.


Packages in Go can be categorized into two types: executable packages and library packages.


Executable packages: These are packages that contain a main function and can be executed directly. When you build an executable package, Go produces an executable binary that can be run from the command line.


Library packages: These are packages that do not contain a main function and are intended to be imported and used by other packages. Library packages encapsulate reusable functionality and can be imported into other packages using the import keyword.


Go provides a standard directory structure for organizing packages within a workspace. Each package is stored in its own directory under the src directory, and the directory structure reflects the package’s import path.


Here’s an example of the directory structure for a Go workspace:


??? myproject/
?   ??? main.go          // Executable package
?   ??? lib/
?   ?   ??? package1/
?   ?   ?   ??? file1.go // Library package
?   ?   ?   ??? file2.go
?   ?   ??? package2/
?   ?       ??? file1.go
?   ?       ??? file2.go
??? github.com/
    ??? username/
        ??? mypackage/
            ??? main.go   // Library package

In this example, the myproject directory contains an executable package with a main function, and the lib directory contains library packages that can be imported into other packages. The github.com/username/mypackage directory structure represents an external library package hosted on GitHub.


Organizing code into packages helps promote code reusability, maintainability, and collaboration in Go projects. It also provides clear boundaries between different parts of the codebase and facilitates dependency management and versioning.

Previously at
Flag Argentina
time icon
Over 5 years of experience in Golang. Led the design and implementation of a distributed system and platform for building conversational chatbots.