Skip to main content

Package

  • A package is a collection of related Go files (.go) grouped together.
  • Each Go file must start with a package declaration.
  • The package name determines scope and visibility of functions, variables, and types.
  • Go programs start execution in the main package.

Types of Packages

  1. Executable packages
    • Contain package main.
    • Must have a func main().
    • Compiles into an executable program.
  2. Library packages
    • Any package that is not main.
    • Provides reusable functions/types/constants.
    • Can be imported into other packages.

Creating and Using a Package

Step 1: Create a Library Package

// file: mathutil/mathutil.go
package mathutil

// Add is exported (Capital letter)
func Add(a, b int) int {
return a + b
}

// multiply is unexported (lowercase letter)
func multiply(a, b int) int {
return a * b
}
  • Add starts with capital A → exported → visible outside mathutil.
  • multiply starts with lowercase → unexported → only usable inside mathutil.

Step 2: Use the Package in Main Program

// file: main.go
package main

import (
"fmt"
"myapp/mathutil" // import custom package
)

func main() {
sum := mathutil.Add(5, 7) // ✅ exported function
fmt.Println("Sum:", sum)

// product := mathutil.multiply(5, 7) // ❌ ERROR: not exported
}
  • main.go creates the executable.
  • mathutil is compiled as a library.
  • Only exported identifiers (Add) are accessible in main.

How Go Organizes Packages

  • Each folder = one package.
  • All .go files in a folder must declare the same package name.
  • Import path is based on module path + folder name.

Example project structure:

myapp/
go.mod
main.go
mathutil/
mathutil.go
advanced.go
  • go.mod defines the module name (myapp).
  • Import path becomes: import "myapp/mathutil"

Run the Program

Initialize module (replace myproject with your module name) which will be use to import package.

go mod init myproject

go.mod file is created automatically.

go run main.go

If multiple files, you can run all at once:

go run .

This compiles temporarily in memory and runs.

Visibility Rules in Packages

  1. Exported (Public):
    • Starts with capital letter.
    • Accessible outside the package.
  2. Unexported (Private):
    • Starts with lowercase letter.
    • Only accessible inside the same package.
package mylib

var ExportedVar = 100 // visible outside
var internalVar = 50 // not visible outside

Special Packages

  • fmt → formatting (Println, Sprintf)
  • strings → string manipulation
  • math → math functions
  • os → operating system
  • time → working with dates/times

RAM and Packages

Global/package-level variables:

  • Allocated in heap/global memory.
  • Persist entire program runtime.
package config

var AppName = "MyApp" // lives in global memory (heap)
  • Functions in a package:
    • Live in code segment (read-only).
  • Local variables inside package functions:
    • Stored in stack frame when function is called.

Summary of Package Behavior

ConceptDescriptionMemory Impact
Package declarationGroups files logically
main packageEntry point of programStack + global heap
Exported identifiersCapitalized, publicAllocated normally (stack/heap)
Unexported identifiersLowercase, privateRestricted to package
Global variablesDeclared outside functionsHeap (lifetime = program)
Local variablesInside function/blockStack (unless escape to heap)