Naming and Organization
Go enforces strict rules about how packages are organized in your file system.
One Package per Directory
All .go files within a single directory must belong to the same package. If you have files with different package declarations in the same folder, the compiler will return an error.
Naming Conventions
- Directory Name: By convention, the package name should match the name of the directory it resides in.
- Import Path: The name used in code is usually the last element of the import path. For example,
math/randis imported asrand. - Lowercase: Package names should be short, concise, and entirely lowercase. Avoid underscores or mixedCaps.
Structure Example
project/
├── main.go (package main)
├── parser/
│ ├── parser.go (package parser)
│ └── utils.go (package parser)
In this example, both files in the parser/ directory must declare package parser.