Packages, Modules, and the main Function
When you first start learning Go, the syntax might look simple — but beneath the surface are a few extremely important rules that every Go program must follow.
- •How Go organizes code using packages
- •Why the main package is special and how it works
- •What the main function is and why it's essential
- •How import statements and the standard library work
- •What Go modules are and how they're used
- •How to build an executable using go build
The Three Key Parts of Every Go Program
Let's look at a simple Go file:
package main
import "fmt"
func main() {
fmt.Print("Hello world")
}This program already contains the fundamental components of any valid Go program:
Defines which package the file belongs to
Brings in external functionality (optional)
Entry point of the program
1. Packages — How Go Organizes Code
At the top of every Go file, you must declare a package:
package mainIf you remove this line, Go will throw an error:
Expected package clause but found 'import'...What is a package?
A package is simply a way to group related Go files together.
- • Every Go file belongs to exactly one package
- • A package may span multiple files
- • Examples:
fmt,http,users,main
Why main?
Because main is a special package.
When a Go program is compiled into an executable, Go needs to know: "Where does the program start?"
If your package is named main, Go treats it as the entry point. If it isn't named main, Go will not produce an executable.
// If you rename it to:
package app
// And run:
go build
// → No executable output, because Go found no entry point2. The import Statement — Bringing in External Functionality
The line:
import "fmt"brings in Go's built-in fmt (format) package.
Go's standard library is huge, and fmt provides printing functions such as Print, Println, and Printf.
When you write:
fmt.Print("Hello")you are calling a function from the fmt package. The value "Hello" is a string literal, always enclosed in double quotes (single quotes are not allowed for strings in Go).
Go Standard Library
Go includes an extensive standard library with packages for everything from HTTP servers to file handling, JSON, cryptography, and much more — all built in.
3. The main Function — The Program's Entry Point
Inside the main package, Go expects exactly one function named main:
func main() {
fmt.Print("Hello world")
}This is the first code executed when the program starts.
- 1.There must be one and only one main function per package
- 2.It cannot take arguments and cannot return a value
- 3.Go does not execute code that sits at the top level (outside functions), unlike languages such as JavaScript
Key Insight
The main function is effectively your program's starting point. Without it, Go doesn't know where to start executing.
4. Enter Go Modules — Your Project's Identity
When you run:
go buildin a totally fresh folder, you may get this:
go: cannot find main module...That's because Go requires your project to be part of a module.
A module is simply a Go project with an identity.
You create one with:
go mod init go-youtubeThis generates a go.mod file:
module go-youtube
go 1.23.5From now on, Go recognizes the folder as a module, and go build will work.
Best Practice for Naming Go Modules
The Go ecosystem expects module names to look like this:
go mod init github.com/username/repo
go mod init gitlab.com/username/repo
go mod init example.com/project-nameThis is because your module name doubles as its import path if others (or future you) want to use it as a dependency.
Avoid names like:
go mod init go-youtube
go mod init myapp
go mod init testWhy it's not ideal:
- • If you import your own module later, paths look weird: import "go-youtube/utils"
- • If you move the repo to GitHub later, you break all imports
- • It doesn't tell Go (or other devs) where the module lives
Best practice example
go mod init github.com/go-backend-journey/go-youtubeEven if you never publish it to GitHub, it's still perfectly valid. Go modules don't require the repo to exist — it's just a namespace.
5. Building a Standalone Executable
Once your module exists, you can build your program:
go buildThis produces an executable:
- •On Windows →
go-youtube.exe - •On Mac/Linux →
go-youtube
Double-click or run:
./go-youtubeThe output appears — even on machines without Go installed.
Go's Power
This is one of Go's strongest features: you build real, native executables. No runtime, no dependencies — just a single binary that can run anywhere.
Summary
- Packages: Every Go file belongs to a package. The main package is special — it creates an executable program.
- Imports: Bring functionality from other packages. Go's standard library includes everything from fmt to http.
- main function: Your program's entry point. Must be in the main package, cannot take parameters or return values.
- Modules: Define your project's identity. Create them using go mod init with a fully qualified path.
- Build:
go buildcreates native executables that work without a Go runtime.
Practical Exercise
Practice everything you've learned:
- Create a new folder for your project
- Initialize a Go module with a proper name (e.g.,
github.com/your-name/hello-go) - Create a
main.gofile with package main and main function - Import the
fmtpackage - Print a message that includes your name and why you're learning Go
- Build the program using
go build - Run the generated executable
Bonus challenge:
Try changing package main to package app and run go build. What happens? Why?