Skip to main content

Command Palette

Search for a command to run...

Packages & Modules in GO

Published
4 min read

Go (Golang) organizes code through packages and modules, which help developers build clean, modular, and maintainable programs. Understanding these concepts is crucial for structuring your Go projects effectively.


Packages in Go

A package in Go is simply a collection of source files located in the same directory that share the same package name.

What is a Package?

A package groups related Go source files together. Each .go file inside a folder begins with the same package declaration, allowing them to share functions, types, and variables.

Example:

// File: mathutils/add.go
package mathutils

func Add(a, b int) int {
    return a + b
}
// File: mathutils/subtract.go
package mathutils

func Subtract(a, b int) int {
    return a - b
}

Here, both files belong to the same package mathutils.

Why Use Packages?

Instead of rewriting the same code multiple times, we can group related logic into packages and reuse them.

Benefits of Packages:

  • Modularity: Organize your code into logical components.
  • Reusability: Share code across multiple projects.
  • Avoid Conflicts: Prevent naming collisions between identifiers in different packages.

Declaring a Package

Every Go source file must start with a package declaration.

package main
  • The main package is special — it defines an executable program.
  • The Go compiler looks for a main() function inside this package as the entry point of your application.

Example:

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}

Types of Packages

Go packages can generally be categorized into three types:

TypeDescriptionExample
1. Standard PackagesPackages that come built-in with Gofmt, os, strings, net/http
2. Built-in PackagesInternal packages automatically available without importlen(), make(), append()
3. Third-Party PackagesExternal packages from Go module proxies (e.g., GitHub)"github.com/gin-gonic/gin"

Importing Packages

You can import packages in two ways:

1️. Single Import

import "fmt"

func main() {
    fmt.Println("Single package imported")
}

2️. Multiple Imports

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Square root of 9 is", math.Sqrt(9))
}

Aliasing Packages

You can assign an alias name to a package when importing.

import m "math"

func main() {
    fmt.Println(m.Pi)
}

Blank Imports

Sometimes, you import a package just for its side effects (such as registering a database driver). In such cases, you use the blank identifier _:

import _ "github.com/go-sql-driver/mysql"

This runs the package’s init() function but doesn’t make its exported names available.


Modules in Go

A module is a collection of related Go packages that are versioned together as a single unit. Modules were introduced in Go 1.11 to replace GOPATH-based dependency management.

A module is defined by a go.mod file in the project’s root directory.

The go.mod File declares
  • The module path (the import path prefix for packages)
  • The Go version
  • The dependencies required by the module

Example:

go mod init github.com/sivakalki/mathapp

This command creates a go.mod file like:

module github.com/sivakalki/mathapp

go 1.22

Adding Dependencies

When you import a third-party package and run go mod tidy, Go automatically adds it to the module’s dependency list.

Example:

package main

import (
    "fmt"
    "github.com/google/uuid"
)

func main() {
    id := uuid.New()
    fmt.Println("Generated UUID:", id)
}

After running:

go mod tidy

Go updates the go.mod and creates a go.sum file for version tracking.

Module Benefits

Benefits of Modular Programming in Go:

  • Code Organization: Promotes a clear, maintainable project structure.
  • Reusability: Use packages across multiple modules and projects.
  • Maintainability: Easier debugging and feature isolation.
  • Collaboration: Different teams can independently work on separate modules.
  • Dependency Management: Ensures consistent, reproducible builds across environments.

🧩 Example Project Structure

mathapp/
│
├── go.mod
├── main.go
└── mathutils/
    ├── add.go
    └── subtract.go

main.go

package main

import (
    "fmt"
    "github.com/sivakalki/mathapp/mathutils"
)

func main() {
    sum := mathutils.Add(10, 5)
    diff := mathutils.Subtract(10, 5)

    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", diff)
}

Output:

Sum: 15
Difference: 5

Summary

ConceptDescription
PackageCollection of Go files in the same directory with the same package name.
ModuleGroup of related packages versioned and managed together via go.mod.
ImportBrings functionality from other packages into your code.
Blank ImportRuns a package’s init() without direct usage.
Aliased ImportImports a package under a custom name.