Skip to main content

Command Palette

Search for a command to run...

Understanding Packages in Go

Updated
3 min read
Understanding Packages in Go

In Go, packages are a key component of organizing and structuring code. Think of them like libraries, modules, or namespaces in other programming languages. Packages help you group related code together. When you build a Go program, it consists of one or more packages, and each Go file belongs to a single package.

However, one package can have multiple files. Even if a program is small, it's good practice to split the code across multiple files, all belonging to the same package. For example, if your file is part of the main package, the first line of your Go file should declare it like this:

package main

The main package is special because it represents the entry point of standalone applications in Go. Every Go application has exactly one main package and this is where execution starts.

Package Dependencies

When building a Go program, it's essential to compile packages in the correct order. Packages often rely on other packages, and this dependency determines the sequence in which they must be compiled. Each package is typically stored in its own directory, and if a package is updated, any program or package that depends on it must also be recompiled.

Importing Packages

In Go, you use the import keyword to include functionality from other packages in your code. For example, to use the fmt package, which provides input-output functionality, you simply write:

import "fmt"

This tells Go to include the public functions, types, and other declarations from the fmt package. If you need to import multiple packages, there are a few ways to do it:

import "fmt"
import "os"

Or you can shorten it by using a grouped import statement:

import (
  "fmt"
  "os"
)

This method is more concise, especially when importing multiple packages, and is called "factoring."

Visibility in Packages

Every Go package exposes certain elements (like functions, types, and variables) to other packages. However, Go enforces strict visibility rules based on the case of the identifier:

  • If an identifier starts with an uppercase letter (e.g., MyFunction), it is exported and can be accessed by other packages.

  • If an identifier starts with a lowercase letter (e.g., myFunction), it is only visible within the same package.

This is similar to the concept of public and private variables in object-oriented languages. The package name acts as a namespace, which allows different packages to have identifiers with the same name without conflicts.

For example, if two packages pack1 and pack2 both have a variable called Object, they can be accessed like this:

pack1.Object
pack2.Object

Using Aliases for Packages

Sometimes, you might want to import a package with a shorter or more descriptive name. Go allows you to create an alias for an imported package. For instance, instead of always typing fmt, you could alias it to fm like this:

import fm "fmt"

Now, throughout your code, you'd refer to the fmt package using fm, like this:

fm.Println("Hello, Go!")

Go's "No Unused Code" Rule

Go strictly follows the principle of "no unnecessary code." If you import a package but don't use it anywhere in your code, the Go compiler will throw an error. This rule ensures that your codebase stays clean and efficient.

Using these import techniques and following Go’s visibility rules, you can effectively manage dependencies and organize your code into clear, reusable components. Next, you'll learn how to define and use functions in Go.

26 views

Learn GO

Part 12 of 13

In this series, I will write articles that help you understand go! I am learning go so I am writing this series as a note for myself and hope you also love this series and learn along with me!

Up next

Filenames, Keywords and Identifiers

File Naming Conventions In Go, source code is stored in files with the .go extension. These filenames follow a simple convention—using only lowercase letters. For example, a file might be named educative.go. When you need to use multiple words in the...

More from this blog

Pavan Chindukuri

18 posts