Package Structure

Until now, we have written all our code in a single file. But do you remember the package main we wrote at the top of every file?

Go is a language that enforces packages from the very beginning. Even if you have only one file, it must have a package declaration. Let’s find out why and what it means.


The Meaning of package main

package main

This single line means “This file belongs to the main package.”

In Go, the main package is special. When Go runs a program, it looks for the main() function inside the main package to start. It’s essentially the “front gate” of your program.

package main

func main() {
    // The program starts from here
}

The Meaning of import

Now we can understand why we wrote import "fmt" whenever we used fmt.Println().

import "fmt"

fmt is one of the standard packages pre-made by Go. It is a toolbox that helps with output, input, string formatting, etc.

import means “I will use this package.”

You can also import multiple packages at once:

import (
    "fmt"
    "math"
    "strings"
)

In GoLand, it automatically imports the packages you use and deletes the ones you don’t!


Creating Your Own Package

As your program grows, you can divide your code into multiple packages.

my_project/
├── go.mod              ← Project configuration file
├── main.go             ← main package (entry point)
├── calculator/
│   └── calc.go         ← calculator package
└── greeting/
    └── hello.go        ← greeting package

File calculator/calc.go:

package calculator

func Add(a int, b int) int {
    return a + b
}

There is an important rule here: If a function name starts with a capital letter, it can be used in other packages.

  • Add → Can be used in other packages (Public)
  • add → Can only be used within the same package (Private)

This is Go’s unique way of doing things. While other languages use keywords like public or private, Go decides based on case sensitivity.

Using it in main.go:

package main

import (
    "fmt"
    "my_project/calculator"
)

func main() {
    result := calculator.Add(3, 5)
    fmt.Println(result) // 8
}

The go.mod File

The starting point of a Go project is the go.mod file. This file declares “This is a single Go project.”

module my_project

go 1.21
  • module: The name of this project (used when importing packages)
  • go 1.21: The version of Go being used

This is automatically generated when you create a new project in GoLand.


Go’s Standard Packages

Go provides a very rich set of standard packages:

PackageDescription
fmtOutput, input, and formatting
mathMathematical functions
stringsString manipulation
osOperating system-related functions
net/httpWeb server and HTTP communication
encoding/jsonJSON data processing

A major strength of Go is that you can build a web server using only these standard packages.


What a Real Project Looks Like

my_api/
├── go.mod
├── main.go                  ← Program entry point
├── handler/                 ← Code that handles requests
│   ├── user.go
│   └── product.go
├── model/                   ← Data structure definitions
│   ├── user.go
│   └── product.go
└── util/                    ← Toolset
    └── helper.go

By dividing packages by role, you can quickly find what you need: “User request handling is in the handler package, and data structures are in the model package!”


Using External Packages

You can also use packages created by other developers. In the terminal:

go get github.com/gin-gonic/gin

This will automatically add the package to go.mod, and you can use it in your code immediately.


To summarize:

  • package main = The entry point of the program
  • import = Using other packages
  • Capitalized names = Accessible from other packages (Go-specific rule)
  • go.mod = The project’s configuration file
  • go get = Command to install external packages

Go’s package system is simple yet powerful. Once you understand these rules, you’re ready to build anything with Go!