TExcelTAD
Home › Go › Go Packages & Modules

Go Packages & Modules

As programs grow, you split code into packages, and packages live inside a module. Understanding the two is essential for any Go project bigger than a single file.

Term What it is Defined by
Package a folder of .go files that are compiled together package name at the top of each file
Module a collection of packages versioned together a go.mod file in the root folder
Import path how you refer to a package module path + folder, e.g. example.com/shop/cart

Packages

Every .go file starts with a package clause. All files in one folder must use the same package name, and they can freely use each other's names — even unexported ones. The package main is special: it builds into an executable.

Identifiers are shared across packages only if they are exported (start with a capital letter):

go
package cart

// Item is exported: other packages can use cart.Item.
type Item struct {
	Name  string
	Price float64
}

// Total is exported.
func Total(items []Item) float64 {
	sum := 0.0
	for _, it := range items {
		sum += it.Price
	}
	return round(sum)
}

// round is unexported: only code inside package cart can call it.
func round(x float64) float64 {
	return float64(int(x*100+0.5)) / 100
}

A typical module layout

text
shop/
├── go.mod                  module example.com/shop
├── main.go                 package main
├── cart/
│   ├── cart.go             package cart
│   └── cart_test.go        package cart (tests)
└── internal/
    └── pricing/
        └── tax.go          package pricing (only importable inside shop)

The go.mod file at the root:

text
module example.com/shop

go 1.25.0

And main.go imports the cart package by its full import path:

go
package main

import (
	"fmt"

	"example.com/shop/cart"
)

func main() {
	items := []cart.Item{{Name: "Pen", Price: 1.2}, {Name: "Pad", Price: 3.45}}
	fmt.Println("Total:", cart.Total(items))
}
bash
go run .
Total: 4.65

Note: A folder named internal is special: packages under it can only be imported by code rooted at the parent of internal. Use it for code you don't want other modules to depend on.

Simulating packages in one file

The playground runs a single file, but you can still see how exported names, unexported helpers and package-level state behave. Everything here lives in package main:

go · try it yourself
package main

import "fmt"

// Package-level variable, initialised before main runs.
var taxRate = 0.2

// init functions run automatically after package variables are set.
func init() {
	fmt.Println("init: tax rate is", taxRate)
}

// Exported style name (would be visible to other packages).
func PriceWithTax(net float64) float64 {
	return roundCents(net * (1 + taxRate))
}

// unexported helper
func roundCents(x float64) float64 {
	return float64(int(x*100+0.5)) / 100
}

func main() {
	fmt.Println("main starts")
	fmt.Println(PriceWithTax(9.99))
}
init: tax rate is 0.2
main starts
11.99

Each package may have one or more init() functions. They run once, in dependency order, before main — useful for setting up lookup tables, but avoid heavy work there.

Import forms

go
import (
	"fmt"                    // normal: use as fmt.Println
	str "strings"            // alias: use as str.ToUpper
	_ "image/png"            // blank: run the package's init only
	"example.com/shop/cart"  // a package from your own module
	"github.com/google/uuid" // a third-party package
)

Standard-library imports come first, then a blank line, then other modules — goimports arranges this for you.

Adding a third-party dependency

bash
go get github.com/google/uuid@v1.6.0
go: downloading github.com/google/uuid v1.6.0
go: added github.com/google/uuid v1.6.0

go get records the dependency in go.mod and writes checksums to go.sum. Commit both files. Running go mod tidy afterwards removes anything you no longer import.

text
module example.com/shop

go 1.25.0

require github.com/google/uuid v1.6.0

Package names and design

  • Short, lower-case, single word: cart, http, strconv — no underscores or camelCase.
  • The name is part of every call, so avoid stutter: cart.Total, not cart.CartTotal.
  • Don't create grab-bag packages named util or common; name packages after what they provide.

Package-level names are shared across files

go · try it yourself
package main

import (
	"fmt"
	"strings"
)

// Imagine this block lives in file greet.go...
const defaultName = "friend"

func greeting(name string) string {
	if strings.TrimSpace(name) == "" {
		name = defaultName
	}
	return "Hello, " + name + "!"
}

// ...and this one in main.go. Same package, so no import is needed.
func main() {
	fmt.Println(greeting("Ola"))
	fmt.Println(greeting("   "))
}
Hello, Ola!
Hello, friend!

Test yourself

Which file defines a Go module?

  1. package.json
  2. go.mod
  3. main.go
  4. module.go

Function parse is in package config. Can main call config.parse()?

  1. Yes
  2. No, lower-case names are unexported
  3. Only with an alias
  4. Only in tests

When do init() functions run?

  1. When you call them
  2. Automatically, before main
  3. After main returns
  4. Only during go test