Package
- A package is a collection of related Go files (
.go) grouped together. - Each Go file must start with a
packagedeclaration. - The package name determines scope and visibility of functions, variables, and types.
- Go programs start execution in the
mainpackage.
Types of Packages
- Executable packages
- Contain package main.
- Must have a func main().
- Compiles into an executable program.
- 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
}
Addstarts with capital A → exported → visible outsidemathutil.multiplystarts with lowercase → unexported → only usable insidemathutil.
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.gocreates the executable.mathutilis compiled as a library.- Only exported identifiers (
Add) are accessible inmain.
How Go Organizes Packages
- Each folder = one package.
- All
.gofiles 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.moddefines 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
- Exported (Public):
- Starts with capital letter.
- Accessible outside the package.
- 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 manipulationmath→ math functionsos→ operating systemtime→ 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
| Concept | Description | Memory Impact |
|---|---|---|
| Package declaration | Groups files logically | — |
main package | Entry point of program | Stack + global heap |
| Exported identifiers | Capitalized, public | Allocated normally (stack/heap) |
| Unexported identifiers | Lowercase, private | Restricted to package |
| Global variables | Declared outside functions | Heap (lifetime = program) |
| Local variables | Inside function/block | Stack (unless escape to heap) |