Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow