Introduction to Packages
Start with Introduction to Packages
A package groups Go declarations that belong together. A Go program uses packages to separate responsibilities without placing every function in one file.
What you’ll learn
- Explain the main purpose of introduction to packages.
- Read and run the complete Go example in this lesson.
- Apply the concept and recognize common mistakes.
Understand the idea
Each Go source file begins with a package clause. Files in the same directory normally declare the same package and share package-level declarations. The executable entry point is package main with func main(); reusable libraries use another package name.
import "fmt" lets code refer to an exported name such as fmt.Println. A package name is not necessarily its full import path: the path identifies where Go finds the package, while the declaration supplies its local name.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Tools, syntax and practical notes
Reference: How to Write Go Code.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import (
"fmt"
"strings"
)
func main() {
name := strings.ToUpper("go")
fmt.Println("Language:", name)
}
Output
Language: GOTrace what the program does
The main package imports fmt for printing and strings for text manipulation. strings.ToUpper produces GO, then fmt.Println displays the result.
Avoid common mistakes
- One directory should not mix ordinary package names such as
mainandhelpersin its non-test Go files. - Importing a package does not make its unexported identifiers accessible.
Try it yourself
Import math and print the result of math.Sqrt(81).
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Introduction to Packages in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.