Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Golang Packages and Modules: Organize Code with go.mod

Packages

Every Golang file belongs to a package. A package groups related files in one directory and gives other code a stable import path. Packages are the main unit of organization in Golang projects.

Package names should be short, lowercase, and meaningful. Avoid vague names such as utils or helpers when a specific name such as slug, email, billing, or auth explains the purpose better.

Package Example
package calculator

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

func internalLabel() string {
    return "calculator"
}

Exported Identifiers

Identifiers that start with a capital letter are exported and can be used from other packages. Lowercase identifiers are package-private.

This rule applies to functions, variables, constants, types, struct fields, and methods. A public API should expose only what other packages truly need, while internal helpers should stay lowercase.

IdentifierExported?Meaning
AddYesOther packages can call it
internalLabelNoOnly this package can call it
User.NameYesField appears in other packages and encoders
User.passwordNoField stays internal
Exported Type and Private Field
package users

type User struct {
    ID       int
    Name     string
    password string
}

func NewUser(name, password string) User {
    return User{Name: name, password: password}
}

Imports

Use import to bring another package into the current file. The imported package name is usually the last part of the import path, but packages can choose a different package name internally.

Golang does not allow unused imports. This keeps files clean and prevents stale dependencies from hiding in the code.

Import Packages
package main

import (
    "fmt"
    "strings"

    "example.com/tutorialslogic/golang-demo/internal/slug"
)

func main() {
    title := strings.TrimSpace(" Golang Modules ")
    fmt.Println(slug.Make(title))
}

Modules and go.mod

A module is a versioned collection of packages. The go.mod file declares the module path, Golang version, and dependency requirements.

Create a module with go mod init. The module path is often a repository path, but local learning projects can use a simple example path. Once a module exists, the go command can resolve imports and manage dependencies.

FilePurpose
go.modDeclares module path, Golang version, and direct dependencies.
go.sumStores dependency checksums for reproducible downloads.
Package filesContain the actual source code inside module directories.
Module Commands
go mod init example.com/tutorialslogic/golang-demo
go mod tidy
go test ./...

Dependency Commands

The go command downloads and manages dependencies. Modern Golang updates go.mod as needed when you import and build packages.

go mod tidy is especially important before committing because it adds missing module requirements and removes unused ones. It also updates go.sum when dependency checksums are needed.

CommandPurpose
go get example.com/pkgAdd, upgrade, or change a dependency
go mod tidyClean module requirements
go list -m allList dependencies
go test ./...Test all module packages
go mod why example.com/pkgExplain why a dependency is needed

Project Structure

Small Golang projects can stay simple. Larger services often place command entry points under cmd and private implementation packages under internal.

The internal directory has special meaning. Packages inside it can only be imported by code inside the parent tree, which helps protect implementation details.

Service Layout
golang-demo/
  go.mod
  go.sum
  cmd/
    api/
      main.go
  internal/
    users/
      service.go
      service_test.go
    slug/
      slug.go
  README.md

Package Design Tips

Design packages around responsibility, not around file type. A package named users is usually clearer than separate packages named models, services, and helpers when those files only serve user behavior.

Keep package APIs small. Export constructors, types, and functions that callers need, but keep implementation details private. This makes packages easier to refactor without breaking other code.

Key Takeaways
  • Every Golang file belongs to a package.
  • Capitalized identifiers are exported from a package.
  • go.mod defines the module path, Golang version, and dependencies.
  • go.sum records dependency checksums.
  • go mod tidy keeps dependency metadata clean.
  • internal packages protect implementation details.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.