What is a Package?
Part of the Logic & Flow section of Coddy's GO journey — lesson 51 of 68.
A package in Go is a way to organize and group related code together. Think of it as a folder that contains functions, variables, and types that work together to accomplish a specific purpose.
Every Go file must belong to a package, declared at the very top with the package keyword. The most important package you'll encounter is package main, which is special because it tells Go that this is an executable program with a main function as the entry point.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}To use code from other packages, you need to import them. In the example above, we import the fmt package, which provides formatting and printing functions. The fmt package is part of Go's standard library and comes built-in with the language.
Packages serve two main purposes: they help organize your code into logical groups and allow you to reuse code across different programs. This makes your programs more maintainable and prevents you from writing the same code multiple times.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Cheat sheet
A package in Go organizes and groups related code together. Every Go file must declare a package at the top using the package keyword.
The package main is special - it creates an executable program with a main function as the entry point:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Use import to include code from other packages. The fmt package provides formatting and printing functions and is part of Go's standard library.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and Behaviors6Idiomatic Go: Sets
The Set Idiom in GoCreating a SetAdding to a SetChecking for MembershipRemoving from a SetIterating Over a SetRecap - Unique Usernames9Packages and Scope
What is a Package?Exported vs UnexportedCreating a Simple PackagePackage AliasingThe Blank Identifier `_`The `init` functionRecap - Building a Utility